Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function coffeeLover(arr) {
- let coffees = arr.shift().split(" ");
- let numberOfCommands = Number(arr.shift());
- for (let i = 0; i < numberOfCommands; i++) {
- let tokens = arr[i].split(" ");
- let command = tokens[0];
- if (command === "Include") {
- let coffee = tokens[1];
- coffees.push(coffee);
- } else if (command === "Remove") {
- let direction = tokens[1];
- let count = Number(tokens[2]);
- if (direction === "first") {
- coffees.splice(0, count);
- } else if (direction === "last") {
- coffees.splice(-count, count);
- }
- } else if (command === "Prefer") {
- let index1 = Number(tokens[1]);
- let index2 = Number(tokens[2]);
- if (index1 >= 0 && index1 < coffees.length && index2 >= 0 && index2 < coffees.length) {
- let temp = coffees[index1];
- coffees[index1] = coffees[index2];
- coffees[index2] = temp;
- }
- } else if (command === "Reverse") {
- coffees.reverse();
- }
- }
- console.log("Coffees:");
- console.log(coffees.join(" "));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement