Advertisement
TZinovieva

Shopping List JS Fundamentals

Feb 16th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shoppingList(arr) {
  2.     let initialList = arr.shift().split('!');
  3.     let lastCommand = arr.pop();
  4.  
  5.     for (let i = 0; i < arr.length; i++) {
  6.       let commands = arr[i].split(' ');
  7.       let command = commands[0];
  8.       let product = commands[1];
  9.       let productNewName = commands[2];
  10.  
  11.       if (command === 'Urgent') {
  12.         if (!initialList.includes(product)) {
  13.           initialList.unshift(product);
  14.         }
  15.       }
  16.       if (command === 'Unnecessary') {
  17.         if (initialList.includes(product)) {
  18.           let index = initialList.indexOf(product);
  19.           initialList.splice(index, 1);
  20.         }
  21.       }
  22.       if (command === 'Correct') {
  23.         if (initialList.includes(product)) {
  24.           let index = initialList.indexOf(product);
  25.           initialList[index] = productNewName;
  26.         }
  27.       }
  28.       if (command === 'Rearrange') {
  29.         if (initialList.includes(product)) {
  30.           let index = initialList.indexOf(product);
  31.           initialList.splice(index, 1);
  32.           initialList.push(product);
  33.         }
  34.       }
  35.     }
  36.       console.log(initialList.join(', '));
  37.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement