Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function library(input) {
  2.     let bookNames = input.shift().split('&');
  3.     let result = '';
  4.     while (input[0] !== "Done") {
  5.         let commands = input.shift().split('|');
  6.        
  7.         if (commands[0].includes('Add')) {
  8.             if (bookNames.includes(commands[1])) {
  9.                 continue;
  10.             }
  11.             bookNames.unshift(commands[1]);
  12.         }
  13.         if (commands[0].includes('Take')) {
  14.             if (bookNames.includes(commands[1])) {
  15.             let indexOfBook = bookNames.indexOf(commands[1]);
  16.             bookNames.splice(indexOfBook, 1);
  17.             }
  18.         }
  19.         if (commands[0].includes('Insert')) {
  20.             bookNames.push(commands[1])
  21.            
  22.         }
  23.         if (commands[0].includes('Swap')) {
  24.             let firstBook = commands[1]
  25.             let secondBook = commands[2]
  26.  
  27.             if (bookNames.includes(firstBook) && bookNames.includes(secondBook)) {
  28.                 let firstIndex = bookNames.indexOf(firstBook);
  29.                 let secondIndex = bookNames.indexOf(secondBook);
  30.  
  31.                 bookNames[firstIndex] = secondBook;
  32.                 bookNames[secondIndex] = firstBook;
  33.             }
  34.         }
  35.     }
  36.  
  37.     console.log(bookNames.join(', '))    
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement