Advertisement
teofarov13

Untitled

Feb 21st, 2023
626
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":check (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(token)
  56.       let book
  57.       if (index>=0 && index < shelf.length) {
  58.          book = shelf.splice(index, 1)
  59.        
  60.       }
  61.       if(!shelf.includes(book)) {
  62.         console.log(book[0]);
  63.       }
  64.    
  65.       shelf.splice(index,0,book)
  66.  
  67.     }
  68.  
  69.   }
  70.   console.log(shelf.join(", "));
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement