Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(params) {
- params.pop();
- let collection = params.shift().split(" ");
- for (let param of params) {
- let tokens = param.split(" ");
- let command = tokens[0];
- if (command === "Delete") {
- let index = Number(tokens[1]);
- if (index >= 0 && index < collection.length) {
- collection.splice(index + 1, 1);
- }
- } else if (command === "Swap") {
- let word1 = tokens[1];
- let word2 = tokens[2];
- for (let i = 0; i < collection.length; i++) {
- if (collection[i] === word1) {
- collection[i] = word2;
- } else if (collection[i] === word2) {
- collection[i] = word1;
- }
- }
- } else if (command === "Put") {
- let word = tokens[1];
- let index = Number(tokens[2]);
- if (index >= 0 && index < collection.length) {
- collection.splice(index - 1, 0, word);
- }
- } else if (command === "Sort") {
- collection.sort((a, b) => b - a);
- } else if (command === "Replace") {
- let word1 = tokens[1];
- let word2 = tokens[2];
- for (let i = 0; i < collection.length; i++) {
- if (collection[i] === word2) {
- collection[i] = word1;
- }
- }
- }
- }
- console.log(collection.join(" "));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement