Advertisement
teofarov13

Untitled

Feb 21st, 2023
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function book(arr) {
  2.   let shelf = arr.shift().split("&");
  3.   let commands = arr.shift();
  4.   while (commands !== "Done") {
  5.     let currComamnds = commands.split(" | ");
  6.     let command = currComamnds[0];
  7.     let token = currComamnds[1];
  8.     let token1 = currComamnds[2]
  9.  
  10.     switch (command) {
  11.       case "Add Book": add(command, token); break;
  12.       case "Take Book": take(command, token); break;
  13.       case "Swap Books": swap(command, token, token1); break;
  14.       case "Insert Book": insert(command, token); break;
  15.       case "Check Book": (command, token); break;
  16.     }
  17.     commands = arr.shift()
  18.    
  19.    
  20.  
  21.     function add(command, token) {
  22.       if (!shelf.includes(token)) {
  23.         shelf.unshift(token);
  24.       }
  25.     }
  26.  
  27.     function take(command, token) {
  28.       if (shelf.includes(token)) {
  29.         let index = shelf.indexOf(token);
  30.         shelf.splice(index, 1);
  31.       }
  32.     }
  33.  
  34.     function swap(command, token, token1) {
  35.       if (shelf.includes(token) && shelf.includes(token1)) {
  36.         let index1 = shelf.indexOf(token);
  37.         let index2 = shelf.indexOf(token1);
  38.        
  39.  
  40.         let temp = shelf[index1];
  41.         let temp2=shelf[index2]
  42.         shelf[index1] = temp2;
  43.         shelf[index2]=temp
  44.  
  45.       }
  46.     }
  47.  
  48.     function insert(command, token) {
  49.       if (!shelf.includes(token)) {
  50.         shelf.push(token);
  51.       }
  52.     }
  53.  
  54.     function check(command, token) {
  55.       let index = Number(shelf.indexOff(token))
  56.  
  57.       if (shelf.includes(index)) {
  58.         console.log(shelf[index]);
  59.       }
  60.     }
  61.  
  62.   }
  63.   console.log(shelf.join(", "));
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement