-Enigmos-

shoppingList.js

Feb 18th, 2022 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shoppingList(input) {
  2.     let index = 0;
  3.     let list = input[index].split(`!`);
  4.     index++;
  5.     let commandList = input[index];
  6.     index++;
  7.  
  8.     while (commandList !== "Go Shopping!") {
  9.         let commands = commandList.split(` `);
  10.         let command = commands[0];
  11.         let item = commands[1];
  12.  
  13.         if (!list.includes(item) && command === "Urgent") {
  14.             list.unshift(item);
  15.         }
  16.  
  17.         if (list.includes(item)) {
  18.             let itemIndex = list.indexOf(item);
  19.             switch (command) {
  20.                 case "Unnecessary":
  21.                     list.splice(itemIndex, 1);
  22.                     break;
  23.                 case "Correct":
  24.                     let itemNew = commands[2];
  25.                     list[itemIndex] = itemNew;
  26.                     break;
  27.                 case "Rearrange":
  28.                     list.splice(itemIndex, 1);
  29.                     list.push(item);
  30.                     break;
  31.             }
  32.         }
  33.         commandList = input[index];
  34.         index++;
  35.     }
  36.     console.log(list.join(`, `));
  37. }
  38.  
  39. shoppingList(["Tomatoes!Potatoes!Bread",
  40.     "Unnecessary Milk",
  41.     "Urgent Tomatoes",
  42.     "Go Shopping!"]);
  43. shoppingList(["Milk!Pepper!Salt!Water!Banana",
  44.     "Urgent Salt",
  45.     "Unnecessary Grapes",
  46.     "Correct Pepper Onion",
  47.     "Rearrange Grapes",
  48.     "Correct Tomatoes Potatoes",
  49.     "Go Shopping!"]);
Add Comment
Please, Sign In to add comment