Advertisement
pacho_the_python

Untitled

Mar 29th, 2023
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function shopping_list(data) {
  2.     let products = data.shift().split("!")
  3.     data.pop()
  4.     for (let i of data) {
  5.         let command_data = i.split(' ')
  6.         let command = command_data[0]
  7.         if (command === 'Urgent') {
  8.             let current_item = command_data[1]
  9.             if (!products.includes(current_item)) {
  10.                 products.unshift(current_item)
  11.             }
  12.         } else if (command === 'Unnecessary') {
  13.             let elementToRemove = command_data[1]
  14.             if (products.includes(elementToRemove)) {
  15.                 products = products.filter(item => item !== elementToRemove)
  16.             }
  17.         } else if (command === 'Correct') {
  18.             let old_item = command_data[1]
  19.             let new_item = command_data[2]
  20.             if (products.includes(old_item)) {
  21.                 let index = products.indexOf(old_item)
  22.                 products[index] = new_item
  23.  
  24.             }
  25.         } else if (command === 'Rearrange ') {
  26.             let rearrange_item = command_data[1]
  27.             if(products.includes(rearrange_item)) {
  28.                 let rearrange_index = products.indexOf(rearrange_item)
  29.                 products.splice(rearrange_index, 1)
  30.                 products.push(rearrange_item)
  31.             }
  32.         }
  33.     }
  34.     console.log(products.join(', '))
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement