Advertisement
cecko

Untitled

Feb 18th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. function shoppingList(arr) {
  2.  
  3. let groceries = arr.shift().split('!');
  4. let shoppingList = groceries;
  5. let i = 0;
  6. while (arr[i] !== "Go Shopping!") {
  7. let tokens = arr[i].split(' ');
  8. let command = tokens[0]; // команди
  9. let index1 = tokens[1];
  10. if (command === "Urgent") {
  11. if (shoppingList.includes(index1)) {
  12.  
  13. } else {
  14. shoppingList.unshift(index1);
  15. }
  16. } else if (command === "Unnecessary") {
  17. if (shoppingList.includes(index1)) {
  18. let indexOfItem = arr.indexOf(index1);
  19. shoppingList.splice(indexOfItem, 1);
  20. }
  21. } else if (command === "Correct") {
  22. if (shoppingList.includes(index1)) {
  23. let indexOfItem = arr.indexOf(index1);
  24. let index2 = tokens[2];
  25. shoppingList.splice(indexOfItem, 1, index2);
  26. }
  27. } else if (command === "Rearrange") {
  28. if (shoppingList.includes(index1)) {
  29. let indexOfItem = arr.indexOf(index1);
  30. shoppingList.splice(indexOfItem, 1);
  31. shoppingList.push(index1);
  32. }
  33. }
  34. i++;
  35. }
  36. console.log(shoppingList.join(', '))
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement