Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input = []){
  2.  
  3.     let library = input.shift().split(`&`);
  4.  
  5.     for(let i = 0; i < input.length; i++){
  6.  
  7.         let tokens = input[i].split(` | `);
  8.  
  9.         let command = tokens[0];
  10.         let firstBook = tokens[1];
  11.         let secondBook = tokens[2];
  12.  
  13.         if(command === `Add Book`){
  14.             if(!library.includes(firstBook)){
  15.                 library.unshift(firstBook);
  16.             }
  17.         }
  18.         else if(command === `Take Book`){
  19.             if(library.includes(firstBook)){
  20.                 let indexOfTheBook = library.indexOf(firstBook);
  21.                 library.splice(indexOfTheBook,1);
  22.             }
  23.         }
  24.         else if(command === `Swap Books`){
  25.             if(library.includes(firstBook, secondBook)){
  26.                 let indexOfTheBook = library.indexOf(firstBook);
  27.                 let indexOfTheSecondBook = library.indexOf(secondBook);
  28.  
  29.  
  30.                 library[indexOfTheBook] = secondBook;
  31.                 library[indexOfTheSecondBook] = firstBook;
  32.             }
  33.         }
  34.         else if(command === `Insert Book`){
  35.             library.push(firstBook);
  36.         }
  37.         else if(command === `Check Book`){
  38.             let searchIndex = firstBook;
  39.            
  40.             if(searchIndex > 0 && searchIndex < library.length){
  41.                 console.log(library[i]);
  42.             }
  43.         }
  44.         else if(command === `Done`){
  45.             console.log(`${library.join(`, `)}`);
  46.         }
  47.     }
  48. }
  49.  
  50. solve([`Don Quixote&The Great Gatsby&Moby Dick&Hamlet`,
  51. `Add Book | The Odyssey`,
  52. `Take Book | Don Quixote`,
  53. `Insert Book | Alice's Adventures in Wonderland`,
  54. `Check Book | 3`,
  55. `Done`
  56. ])
  57.  
  58.  
  59. solve([`Anna Karenina&Heart of Darkness&Catch-22& The Stranger`,
  60. `Add Book | David Copperfield`,
  61. `Add Book | One Thousand and One Nights`,
  62. `Swap Books | One Thousand and One Nights | Catch-22`,
  63. `Take Book | David Copperfield`,
  64. `Insert Book | The Stories of Anton Chekhov`,
  65. `Check Book | 17`,
  66. `Done`
  67. ])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement