Neri0817

02. Shopping List

Feb 16th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shoppingList(array) {
  2.   let list = array.shift().split("!");
  3.   let copyCommand = array.slice();
  4.  
  5.   for (let i = 0; i < copyCommand.length; i++) {
  6.     if (array[i] === "Go Shopping!") {
  7.       break;
  8.     }
  9.  
  10.     let current = array[i].split(" ");
  11.     let command = current.shift();
  12.  
  13.     if (command === "Urgent") {
  14.       let item = current.shift();
  15.  
  16.       if (!list.includes(item)) {
  17.         list.unshift(item);
  18.       }
  19.     } else if (command === "Unnecessary") {
  20.       let item = current.shift();
  21.  
  22.       if (list.includes(item)) {
  23.         let index = list.indexOf(item);
  24.         list.splice(index, 1);
  25.       }
  26.     } else if (command === "Correct") {
  27.       let oldItem = current.shift();
  28.       let newItem = current.shift();
  29.  
  30.       if (list.includes(oldItem)) {
  31.         let index = list.indexOf(oldItem);
  32.         list.splice(index, 1, newItem);
  33.       }
  34.     } else if (command === "Rearrange") {
  35.       let item = current.shift();
  36.       if (list.includes(item)) {
  37.         let index = list.indexOf(item);
  38.         let change = list.splice(index, 1);
  39.         list.push(change);
  40.       }
  41.     }
  42.   }
  43.   console.log(list.join(", "));
  44. }
  45.  
  46. // shoppingList([
  47. //   "Tomatoes!Potatoes!Bread",
  48. //   "Unnecessary Milk",
  49. //   "Urgent Tomatoes",
  50. //   "Go Shopping!",
  51. // ]);
  52.  
  53. shoppingList([
  54.   "Milk!Pepper!Salt!Water!Banana",
  55.   "Urgent Salt",
  56.   "Unnecessary Grapes",
  57.   "Correct Pepper Onion",
  58.   "Rearrange Grapes",
  59.   "Correct Tomatoes Potatoes",
  60.   "Go Shopping!",
  61. ]);
  62.  
Add Comment
Please, Sign In to add comment