Advertisement
Spocoman

02. Shopping List

Nov 9th, 2023 (edited)
935
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shoppingList(input) {
  2.     let list = input.shift().split('!');
  3.  
  4.     while (input[0] !== "Go Shopping!") {
  5.         [command, product, newProduct] = input.shift().split(' ');
  6.         if (command === "Urgent" && !list.includes(product)) {
  7.             list.unshift(product);
  8.         } else if (command === "Unnecessary" && list.includes(product)) {
  9.             list.splice(list.indexOf(product), 1);
  10.         } else if (command === "Correct" && list.includes(product)) {
  11.             list.splice(list.indexOf(product), 1, newProduct);
  12.         } else if (command === "Rearrange" && list.includes(product)) {
  13.             list.push(list.splice(list.indexOf(product), 1));
  14.         }
  15.     }  
  16.     console.log(list.join(", "));
  17.     return;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement