Advertisement
PPetkov2000

Programming Fundamentals: The Final Quest

Oct 23rd, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(params) {
  2.   params.pop();
  3.   let collection = params.shift().split(" ");
  4.  
  5.   for (let param of params) {
  6.     let tokens = param.split(" ");
  7.     let command = tokens[0];
  8.  
  9.     if (command === "Delete") {
  10.       let index = Number(tokens[1]);
  11.       if (index >= 0 && index < collection.length) {
  12.         collection.splice(index + 1, 1);
  13.       }
  14.     } else if (command === "Swap") {
  15.       let word1 = tokens[1];
  16.       let word2 = tokens[2];
  17.       for (let i = 0; i < collection.length; i++) {
  18.         if (collection[i] === word1) {
  19.           collection[i] = word2;
  20.         } else if (collection[i] === word2) {
  21.           collection[i] = word1;
  22.         }
  23.       }
  24.     } else if (command === "Put") {
  25.       let word = tokens[1];
  26.       let index = Number(tokens[2]);
  27.       if (index >= 0 && index < collection.length) {
  28.         collection.splice(index - 1, 0, word);
  29.       }
  30.     } else if (command === "Sort") {
  31.       collection.sort((a, b) => b - a);
  32.     } else if (command === "Replace") {
  33.       let word1 = tokens[1];
  34.       let word2 = tokens[2];
  35.       for (let i = 0; i < collection.length; i++) {
  36.         if (collection[i] === word2) {
  37.           collection[i] = word1;
  38.         }
  39.       }
  40.     }
  41.   }
  42.   console.log(collection.join(" "));
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement