SHARE
TWEET

School Library kozmoza

AreWe Jun 30th, 2020 1,078 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let bookShelves = input.shift().split("&");
  3.    
  4.       for (book of input) {
  5.           let [currentCommand, currentBook, currentBook2] = book.split(" | ");
  6.    
  7.           if (currentCommand === 'Add Book') {
  8.               if (!bookShelves.includes(currentBook)) {
  9.                   bookShelves.unshift(currentBook);
  10.               }
  11.           } else if (currentCommand === 'Take Book') {
  12.                 bookShelves = bookShelves.filter(book => book !== currentBook);
  13.           } else if (currentCommand ==='Insert Book') {
  14.                 bookShelves.push(currentBook);
  15.           } else if (currentCommand === 'Check Book') {
  16.               let index = Number(currentBook);
  17.               if(bookShelves[index] !== undefined) {
  18.                   console.log(bookShelves[index]);
  19.               }
  20.           } else if(currentCommand === 'Swap Books') {
  21.               let [idx1, idx2] = [bookShelves.indexOf(currentBook), bookShelves.indexOf(currentBook2)];
  22.               if(idx1 !== -1 && idx2 !== -1) {
  23.                   [bookShelves[idx1], bookShelves[idx2]] = [currentBook2, currentBook];
  24.               }
  25.           } else if (currentCommand === 'Done') {
  26.               console.log(bookShelves.join(', '));
  27.               break;
  28.           }
  29.       }
  30.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top