Liliana797979

the final quest - mid exam

Aug 15th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function theFinalQuest(input){
  2.     let wordsCollection = input.shift().split(' ');
  3.  
  4.     while(input.length > 0){
  5.         let command = input.shift();
  6.  
  7.         if(command == 'Stop'){
  8.             break;
  9.         }
  10.  
  11.         let curCommand = command.split(' ')[0];
  12.  
  13.         switch(curCommand){
  14.             case 'Delete' :
  15.                 let indexForDelite = Number(command.split(' ')[1]);
  16.                 wordsCollection = deliteCase(wordsCollection, indexForDelite);
  17.                 break;
  18.             case 'Swap' :
  19.                 let firstWord = command.split(' ')[1];
  20.                 let secondWord = command.split(' ')[2];
  21.                 wordsCollection = swapCase(wordsCollection, firstWord, secondWord);
  22.                 break;
  23.             case 'Put' :
  24.                 let wordPutCase = command.split(' ')[1];
  25.                 let indexPutCase = Number(command.split(' ')[2]);
  26.                 wordsCollection = putCase(wordsCollection, wordPutCase, indexPutCase);
  27.                 break;
  28.             case 'Sort':
  29.                 wordsCollection = wordsCollection.sort((a, b) => b.localeCompare(a));
  30.                 break;
  31.             case 'Replace' :
  32.                 let firstWordReplace = command.split(' ')[1];
  33.                 let secondWordReplace = command.split(' ')[2];
  34.                 wordsCollection = replaceCase(wordsCollection, firstWordReplace, secondWordReplace);
  35.                 break;
  36.  
  37.         }
  38.     }
  39.  
  40.     console.log(wordsCollection.join(' '));
  41.  
  42.     function deliteCase(arr, index){
  43.         if(index + 1 > 0 && index + 1 < arr.length){
  44.             arr = arr.filter((x,y) => y != index + 1);
  45.         }
  46.         return arr;
  47.     }
  48.  
  49.     function swapCase(arr, first, second){
  50.         let firstSwap = second;
  51.         let secondSwap = first;
  52.  
  53.         if(arr.includes(first) && arr.includes(second)){
  54.             for(let i = 0; i < arr.length; i++){
  55.                 if(arr[i] == first){
  56.                     arr[i] = firstSwap;
  57.                 } else if (arr[i] == second){
  58.                     arr[i] = secondSwap;
  59.                 }
  60.             }
  61.         }
  62.         return arr;
  63.     }
  64.  
  65.     function putCase(arr, word, index){
  66.         if(index - 1 > 0 && index - 1 < arr.length){
  67.             arr.splice(index - 1,0,word);
  68.         }
  69.         return arr;
  70.     }
  71.  
  72.     function replaceCase(arr, first, second){
  73.         if(arr.includes(second)){
  74.             for(let i = 0; i < arr.length; i++){
  75.                 if(i == arr.indexOf(second)){
  76.                     arr[i] = first;
  77.                 }
  78.             }
  79.         }
  80.         return arr;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment