Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function shoppingList(arr) {
- let groceries = arr.shift().split('!');
- let shoppingList = groceries;
- let i = 0;
- while (arr[i] !== "Go Shopping!") {
- let tokens = arr[i].split(' ');
- let command = tokens[0]; // команди
- let index1 = tokens[1];
- if (command === "Urgent") {
- if (shoppingList.includes(index1)) {
- } else {
- shoppingList.unshift(index1);
- }
- } else if (command === "Unnecessary") {
- if (shoppingList.includes(index1)) {
- let indexOfItem = arr.indexOf(index1);
- shoppingList.splice(indexOfItem, 1);
- }
- } else if (command === "Correct") {
- if (shoppingList.includes(index1)) {
- let indexOfItem = arr.indexOf(index1);
- let index2 = tokens[2];
- shoppingList.splice(indexOfItem, 1, index2);
- }
- } else if (command === "Rearrange") {
- if (shoppingList.includes(index1)) {
- let indexOfItem = arr.indexOf(index1);
- shoppingList.splice(indexOfItem, 1);
- shoppingList.push(index1);
- }
- }
- i++;
- }
- console.log(shoppingList.join(', '))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement