Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function coffeeLover(input) {
- let index = 0;
- let coffeeList = input[index].split(` `);
- index++;
- let cmdCounter = Number(input[index]);
- index++;
- for (let i = 0; i < cmdCounter; i++) {
- let cmdList = input[index].split(` `);
- index++;
- let cmd = cmdList[0];
- let value = cmdList[1];
- switch(cmd) {
- case "Include":
- coffeeList.push(value);
- break;
- case "Remove":
- if (value === "first") {
- coffeeList.splice(0, Number(cmdList[2]));
- } else if (value === "last") {
- coffeeList.splice(-1, Number(cmdList[2]));
- }
- break;
- case "Prefer":
- if (coffeeList[Number(value)] !== undefined && coffeeList[Number(cmdList[2])] !== undefined) {
- let index = Number(value);
- let index2 = Number(cmdList[2]);
- let indexBackup = coffeeList[index2];
- coffeeList[index2] = coffeeList[index];
- coffeeList[index] = indexBackup;
- }
- break;
- case "Reverse":
- coffeeList = coffeeList.reverse();
- break;
- }
- }
- console.log("Coffees:");
- console.log(coffeeList.join(` `));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement