Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function shopping_list(data) {
- let products = data.shift().split("!")
- for (let i of data) {
- let command_data = i.split(' ')
- let command = command_data[0]
- if (command === 'Urgent') {
- let current_item = command_data[1]
- if (!products.includes(current_item)) {
- products.unshift(current_item)
- }
- } else if (command === 'Unnecessary') {
- let elementToRemove = command_data[1]
- if (products.includes(elementToRemove)) {
- products = products.filter(item => item !== elementToRemove)
- }
- } else if (command === 'Correct') {
- let old_item = command_data[1]
- let new_item = command_data[2]
- if (products.includes(old_item)) {
- let index = products.indexOf(old_item)
- products[index] = new_item
- }
- } else if (command === 'Rearrange ') {
- let rearrange_item = command_data[1]
- if(products.includes(rearrange_item)) {
- let rearrange_index = products.indexOf(rearrange_item)
- products.splice(rearrange_index, 1)
- products.push(rearrange_item)
- }
- }
- }
- console.log(products.join(', '))
- }
- // shopping_list(
- // ["Milk!Pepper!Salt!Water!Banana",
- // "Urgent Salt",
- // "Unnecessary Grapes",
- // "Correct Pepper Onion",
- // "Rearrange Grapes",
- // "Correct Tomatoes Potatoes",
- // "Go Shopping!"])
- shopping_list(["Tomatoes!Potatoes!Bread",
- "Unnecessary Milk",
- "Urgent Tomatoes",
- "Go Shopping!"])
Add Comment
Please, Sign In to add comment