dilyana2001

problem 3

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