Liliana797979

shopping list mid exam - fundamentals

May 23rd, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function solve(input) {
  3.     let shoppingList = input.shift().split('!');
  4.     let commands = input.shift();
  5.     while (commands !== 'Go Shopping!') {
  6.         let tokens = commands.split(' ');
  7.         let command = tokens[0];
  8.         let item = tokens[1];
  9.         let index = shoppingList.indexOf(item);
  10.         switch (command) {
  11.             case 'Urgent':
  12.                 if (index < 0) {
  13.                     shoppingList.unshift(item);
  14.                 }
  15.                 break;
  16.             case 'Unnecessary':
  17.                 if (~index) {
  18.                     shoppingList.splice(index, 1);
  19.                 }
  20.                 break;
  21.             case 'Correct':
  22.                 let newItem = tokens[2];
  23.                 if (~index) {
  24.                     shoppingList.splice(index, 1, newItem);
  25.                 }
  26.                 break;
  27.             case 'Rearrange':
  28.                 if (~index) {
  29.                     shoppingList.splice(index, 1);
  30.                     shoppingList.push(item);
  31.                 }
  32.                 break;
  33.             default:
  34.                 break;
  35.         }
  36.         commands = input.shift();
  37.     }
  38.     console.log(shoppingList.join(', '));
  39. }
Advertisement
Add Comment
Please, Sign In to add comment