Advertisement
Liliana797979

shopping list1 mid exam - fundamentals

Jun 30th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function shopingList(array) {
  3.     let initialList = array.shift().split("!");
  4.     for (let command of array) {
  5.         let [typeOfCommand, product, newProduct] = command.split(" ");
  6.         if (typeOfCommand === "Urgent") {
  7.             if (!initialList.includes(product)) {
  8.                 initialList.unshift(product);
  9.             }
  10.         } else if (typeOfCommand === "Unnecessary") {
  11.             if (initialList.includes(product)) {
  12.                 let indexOfProduct = initialList.indexOf(product);
  13.                 initialList.splice(indexOfProduct, 1);
  14.             }
  15.         } else if (typeOfCommand === "Correct") {
  16.             if (initialList.includes(product)) {
  17.                 let indexOfProduct = initialList.indexOf(product);
  18.                 initialList[indexOfProduct] = newProduct;
  19.             }
  20.  
  21.         } else if (typeOfCommand === "Rearrange") {
  22.             if (initialList.includes(product)) {
  23.                 let indexOfProduct = initialList.indexOf(product);
  24.                 let item = initialList.splice(indexOfProduct, 1);
  25.                 initialList.push(item);
  26.             }
  27.         } else {
  28.             break;
  29.         }
  30.  
  31.     }
  32.     console.log(initialList.join(", "));
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement