Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function shoppingList(input) {
- let index = 0;
- let list = input[index].split(`!`);
- index++;
- let commandList = input[index];
- index++;
- while (commandList !== "Go Shopping!") {
- let commands = commandList.split(` `);
- let command = commands[0];
- let item = commands[1];
- if (!list.includes(item) && command === "Urgent") {
- list.unshift(item);
- }
- if (list.includes(item)) {
- let itemIndex = list.indexOf(item);
- switch (command) {
- case "Unnecessary":
- list.splice(itemIndex, 1);
- break;
- case "Correct":
- let itemNew = commands[2];
- list[itemIndex] = itemNew;
- break;
- case "Rearrange":
- list.splice(itemIndex, 1);
- list.push(item);
- break;
- }
- }
- commandList = input[index];
- index++;
- }
- console.log(list.join(`, `));
- }
- shoppingList(["Tomatoes!Potatoes!Bread",
- "Unnecessary Milk",
- "Urgent Tomatoes",
- "Go Shopping!"]);
- shoppingList(["Milk!Pepper!Salt!Water!Banana",
- "Urgent Salt",
- "Unnecessary Grapes",
- "Correct Pepper Onion",
- "Rearrange Grapes",
- "Correct Tomatoes Potatoes",
- "Go Shopping!"]);
Add Comment
Please, Sign In to add comment