Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
97
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.     while (input.length > 0) {
  5.         let commands = input.shift().split(' | ');
  6.         let action = commands[0];
  7.         let bookName = commands[1];
  8.         let book2Name = commands[2];
  9.  
  10.         if (action === 'Add Book') {
  11.             if (arr.includes(bookName)) {
  12.                 continue;
  13.             } else {
  14.                 arr.unshift(bookName);
  15.             }
  16.         } else if (action === 'Take Book') {
  17.             if (arr.includes(bookName)) {
  18.                 let index = arr.indexOf(bookName);
  19.                 arr.splice(index, 1);
  20.             }
  21.         } else if (action === 'Swap Books') {
  22.             if (arr.includes(bookName) && arr.includes(book2Name)) {
  23.                 let index1 = arr.indexOf(bookName);
  24.                 let index2 = arr.indexOf(book2Name);
  25.                 let temp = arr[index1];
  26.                 arr[index1] = arr[index2];
  27.                 arr[index2] = temp;
  28.             }
  29.         } else if (action === 'Insert Book') {
  30.             arr.push(bookName);
  31.         } else if (action === 'Check Book') {
  32.             let index = Number(bookName);
  33.             if ( 0 < index && index < arr.length) {
  34.                 console.log(arr[index]);
  35.             }
  36.         }
  37.  
  38.     }
  39.     console.log(arr.join(', '));
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement