Advertisement
pacho_the_python

Untitled

Mar 29th, 2023
728
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.  
  4.     let counter = 0
  5.     while (true) {
  6.         if (data[counter] === 'Go Shopping!') {
  7.             console.log(products.join(', '))
  8.             break
  9.         }
  10.  
  11.         let command_data = data[counter].split(' ')
  12.         let command = command_data[0]
  13.  
  14.         if (command === 'Urgent') {
  15.             let current_item = command_data[1]
  16.             if (!products.includes(current_item)) {
  17.                 products.unshift(current_item)
  18.                 }
  19.             } else if (command === 'Unnecessary') {
  20.                  let elementToRemove = command_data[1]
  21.                  if (products.includes(elementToRemove)) {
  22.                      let index = products.indexOf(elementToRemove)
  23.                      products.splice(index, 1)
  24.                  }
  25.             } else if (command === 'Correct') {
  26.                  let old_item = command_data[1]
  27.                  let new_item = command_data[2]
  28.                  if (products.includes(old_item)) {
  29.                      let index = products.indexOf(old_item)
  30.                      products.splice(index, 1, new_item)
  31.                  }
  32.             } else if (command === 'Rearrange ') {
  33.                 let rearrange_item = command_data[1]
  34.                 if(products.includes(rearrange_item)) {
  35.                 let index = products.indexOf(rearrange_item);
  36.                 products.splice(index, 1);
  37.                 products.push(rearrange_item);
  38.                 }
  39.             }
  40.         counter += 1
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement