dimoBs

shopping list

Feb 23rd, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //02. Shopping List
  2. function solve(array) {
  3.  
  4.     let list = array.shift().split('!')
  5.  
  6.     for (let line in array) {
  7.         let command = array[line];
  8.  
  9.         while (command !== 'Go Shopping!'){
  10.             let input = array[line].split(' ');
  11.             let items = input[1];
  12.  
  13.             switch (input[0]) {
  14.  
  15.                 case 'Unnecessary':
  16.                     if (list.includes(items)) {
  17.                         list.splice(list.indexOf(items), 1)
  18.                     }
  19.                     break;
  20.  
  21.                 case 'Urgent':
  22.                     if (!list.includes(items)) {
  23.                         list.unshift(items);
  24.                     }
  25.                     break;
  26.  
  27.                     case 'Correct':
  28.                       let oldItems = input[1];
  29.                       let newItems = input[2];
  30.                         if(list.includes(oldItems)){
  31.                            list.splice(list.indexOf(oldItems), 1, (newItems));
  32.                         }
  33.                         break;
  34.  
  35.                         case 'Rearrange':
  36.                             if(list.includes(items)){
  37.                                 list.push(items);
  38.                                 list.splice(list.indexOf(items), 1);
  39.                             }
  40.                             break;
  41.             }
  42.          break;
  43.         }
  44.     }
  45.     console.log(list.join(', '));
  46.     }
  47. // solve(['Tomatoes!Potatoes!Bread','Unnecessary Milk','Urgent Tomatoes',]);
  48. solve(['Milk!Pepper!Salt!Water!Banana','Urgent Salt','Go Shopping!','Correct Pepper Onion',
  49. 'Rearrange Onion','Correct Tomatoes Potatoes','Go Shopping!'])
Add Comment
Please, Sign In to add comment