pacho_the_python

Untitled

Mar 29th, 2023
98
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.     for (let i of data) {
  5.         let command_data = i.split(' ')
  6.         let command = command_data[0]
  7.        
  8.         if (command === 'Urgent') {
  9.             let current_item = command_data[1]
  10.             if (!products.includes(current_item)) {
  11.                 products.unshift(current_item)
  12.             }
  13.         } else if (command === 'Unnecessary') {
  14.             let elementToRemove = command_data[1]
  15.             if (products.includes(elementToRemove)) {
  16.                 products = products.filter(item => item !== elementToRemove)
  17.             }
  18.         } else if (command === 'Correct') {
  19.             let old_item = command_data[1]
  20.             let new_item = command_data[2]
  21.             if (products.includes(old_item)) {
  22.                 let index = products.indexOf(old_item)
  23.                 products[index] = new_item
  24.  
  25.             }
  26.         } else if (command === 'Rearrange ') {
  27.             let rearrange_item = command_data[1]
  28.             if(products.includes(rearrange_item)) {
  29.                 let rearrange_index = products.indexOf(rearrange_item)
  30.                 products.splice(rearrange_index, 1)
  31.                 products.push(rearrange_item)
  32.             }
  33.         }
  34.     }
  35.     console.log(products.join(', '))
  36. }
  37.  
  38. // shopping_list(
  39. //     ["Milk!Pepper!Salt!Water!Banana",
  40. //         "Urgent Salt",
  41. //         "Unnecessary Grapes",
  42. //         "Correct Pepper Onion",
  43. //         "Rearrange Grapes",
  44. //         "Correct Tomatoes Potatoes",
  45. //         "Go Shopping!"])
  46.  
  47. shopping_list(["Tomatoes!Potatoes!Bread",
  48.     "Unnecessary Milk",
  49.     "Urgent Tomatoes",
  50.     "Go Shopping!"])
Add Comment
Please, Sign In to add comment