Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 03. Easter Shopping
- function easterShopping(input) {
- shopsArr = input.shift().split(" ");
- let commandsArr = input.slice(1);
- for (let i = 0; i < commandsArr.length; i++) {
- let currentAction = commandsArr[i].split(" ");
- let action = currentAction[0];
- if (action.includes("Include")) {
- let newShop = currentAction[1];
- shopsArr.push(newShop);
- } else if (action.includes("Visit")) {
- let numberOfShopsToRemove = Number(currentAction[2]);
- if (shopsArr.length >= numberOfShopsToRemove) {
- if (currentAction.includes("first")) {
- shopsArr.splice(0, numberOfShopsToRemove);
- } else if (currentAction.includes("last")) {
- for (let j = 0; j < numberOfShopsToRemove; j++) {
- shopsArr.pop();
- }
- }
- }
- } else if (action.includes("Prefer")) {
- let indexOfFirstShop = Number(currentAction[1]);
- let indexOfSecondShop = Number(currentAction[2]);
- if (indexOfFirstShop >= 0 && indexOfFirstShop < shopsArr.length && indexOfSecondShop >= 0 &&
- indexOfSecondShop < shopsArr.length) {
- let firstShop = shopsArr[indexOfFirstShop];
- let secondShop = shopsArr[indexOfSecondShop];
- shopsArr.splice(indexOfFirstShop, 1, secondShop);
- shopsArr.splice(indexOfSecondShop, 1, firstShop);
- }
- } else if (action.includes("Place")) {
- let indexToPlaceAfter = Number(currentAction[2]);
- let indexToPlaceAt = indexToPlaceAfter + 1;
- if (indexToPlaceAt >= 0 && indexToPlaceAt < shopsArr.length) {
- shopsArr.splice(indexToPlaceAt, 0, currentAction[1]);
- }
- }
- }
- console.log("Shops left:")
- console.log(shopsArr.join(" "));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement