Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve3 (input=[]) {
  2.     let arr = input.shift().split('&');
  3.  
  4.     // let commands = input.shift().split(' | ');
  5.  
  6.     while ( input.length > 0      /*commands[0] !== 'Done'*/) {
  7.         let commands = input.shift().split(' | ');
  8.         let action = commands[0];
  9.         let bookName = commands[1];
  10.         let book2Name = commands[2];
  11.  
  12.         if (action === 'Add Book') {
  13.             if (arr.includes(bookName)) {
  14.                 continue;
  15.             } else {
  16.                 arr.unshift(bookName);
  17.             }
  18.         } else if (action === 'Take Book') {
  19.             if (arr.includes(bookName)) {
  20.                 let index = arr.indexOf(bookName);
  21.                 arr.splice(index, 1);
  22.             }
  23.         } else if (action === 'Swap Books') {
  24.             if (arr.includes(bookName) && arr.includes(book2Name)) {
  25.                 let index1 = arr.indexOf(bookName);
  26.                 let index2 = arr.indexOf(book2Name);
  27.                 let temp = arr[index1];
  28.                 arr[index1] = arr[index2];
  29.                 arr[index2] = temp;
  30.             }
  31.         } else if (action === 'Insert Book') {
  32.             arr.push(bookName);
  33.         } else if (action === 'Check Book') {
  34.             let index = Number(bookName);
  35.             if ( 0 < index && index < arr.length) {
  36.                 console.log(arr[index]);
  37.             }
  38.         }
  39.  
  40.         // commands = input.shift().split(' | ');
  41.     }
  42.     console.log(arr.join(', '));
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement