Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function arrayModify(data) {
- let arr = data.shift().split(" ").map(Number);
- let index = 0;
- let command = data[index];
- index++;
- while (command !== "end") {
- let tokens = command.split(" ");
- let action = tokens[0];
- let indexOfFirstEl = Number(tokens[1]);
- let indexOfSecondEl = Number(tokens[2]);
- switch (action) {
- case "swap":
- let elementToReplace = arr[indexOfFirstEl];
- let replacedElement = arr[indexOfSecondEl];
- arr.splice(indexOfFirstEl, 1, replacedElement);
- arr.splice(indexOfSecondEl, 1, elementToReplace);
- break;
- case "multiply":
- arr[indexOfFirstEl] *= arr[indexOfSecondEl];
- break;
- case "decrease":
- arr = arr.map((element) => element - 1);
- break;
- }
- command = data[index];
- index++;
- }
- console.log(arr.join(", "));
- }
Advertisement
Add Comment
Please, Sign In to add comment