Advertisement
knoteva

Untitled

Oct 24th, 2019
383
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.       if(collection.indexOf(word1) > -1 && collection.indexOf(word2) > -1) { // проверка дали думите са в листа
  18.       for (let i = 0; i < collection.length; i++) {
  19.         if (collection[i] === word1) {
  20.           collection[i] = word2;
  21.         } else if (collection[i] === word2) {
  22.           collection[i] = word1;
  23.         }
  24.       }
  25.       }
  26.     } else if (command === "Put") {
  27.       let word = tokens[1];
  28.       let index = Number(tokens[2]);
  29.       if (index >= 1 && index - 1 <= collection.length) {// Вярна проверка за индекса
  30.         collection.splice(index - 1, 0, word);
  31.       }
  32.     } else if (command === "Sort") {
  33.       collection.sort().reverse(); // Правилно сортиране
  34.     } else if (command === "Replace") {
  35.       let word1 = tokens[1];
  36.       let word2 = tokens[2];
  37.       for (let i = 0; i < collection.length; i++) {
  38.         if (collection[i] === word2) {
  39.           collection[i] = word1;
  40.         }
  41.       }
  42.     }
  43.   }
  44.   console.log(collection.join(" "));
  45. }
  46.  
  47. solve([
  48.   'Congratulations! You last also through the have challenge!',
  49.   'Swap hve lat',
  50.   'Stop']
  51. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement