Liliana797979

problem 3_1 - mid exam

Jul 22nd, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.      
  2. function solve(arr) {
  3.     let listOfProducts = arr.shift().split('|')
  4.     for (let line of arr) {
  5.         let [command, ...args] = line.split('%')
  6.         if (command == 'Important') {
  7.             if (listOfProducts.includes(args[0])) {
  8.                 let index = listOfProducts.indexOf(args[0])
  9.                 listOfProducts.splice(index, 1)
  10.             }
  11.             listOfProducts.unshift(args[0])
  12.         } else if (command == 'Add') {
  13.             if (!listOfProducts.includes(args[0])) {
  14.                 listOfProducts.push(args[0])
  15.             } else {
  16.                 console.log(`The product is already in the list.`);
  17.             }
  18.         } else if (command == 'Swap') {
  19.             if (listOfProducts.includes(args[0]) && listOfProducts.includes(args[1])) {
  20.                 let firstIndex = listOfProducts.indexOf(args[0])
  21.                 let secondIndex = listOfProducts.indexOf(args[1])
  22.                 let swap = listOfProducts[firstIndex]
  23.                 listOfProducts[firstIndex] = listOfProducts[secondIndex]
  24.                 listOfProducts[secondIndex] = swap
  25.             } else if (!listOfProducts.includes(args[0])) {
  26.                 console.log(`Product ${args[0]} missing!`);
  27.             } else if (!listOfProducts.includes(args[1])) {
  28.                 console.log(`Product ${args[1]} missing!`);
  29.             }
  30.         } else if (command == 'Remove') {
  31.             if (listOfProducts.includes(args[0])) {
  32.                 let index = listOfProducts.indexOf(args[0])
  33.                 listOfProducts.splice(index, 1)
  34.             } else {
  35.                 console.log(`Product ${args[0]} isn't in the list.`);
  36.            }
  37.        } else if (command == 'Reversed') {
  38.            listOfProducts.reverse()
  39.        } else {
  40.            listOfProducts.map((x, i) => console.log(`${i+1}. ${x}`))
  41.        }
  42.  
  43.    }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment