Guest User

Untitled

a guest
Jun 27th, 2019
45
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveTheRiddle(arrayInput) {
  2.     const questToSolve = arrayInput.shift().split(' ');
  3.     let currentCommands = arrayInput.shift().split(' ');
  4.  
  5.  
  6.     while (currentCommands[0] !== 'Stop') {
  7.  
  8.         if (currentCommands[0] === 'Delete') {
  9.             const index = +currentCommands[1];
  10.             if (questToSolve.indexOf(questToSolve[index]) !== -1) {
  11.                 questToSolve.splice(index + 1, 1);
  12.             }
  13.         } else if (currentCommands[0] === 'Swap') {
  14.             let word1 = currentCommands[1];
  15.             let word2 = currentCommands[2];
  16.             if (questToSolve.indexOf(word1) && questToSolve.indexOf(word2)) {
  17.                 const word1Index = questToSolve.indexOf(word1);
  18.                 const word2Index = questToSolve.indexOf(word2);
  19.                 const savedFirst = questToSolve[word1Index];
  20.                 questToSolve[word1Index] = word2;
  21.                 questToSolve[word2Index] = word1;
  22.             }
  23.         } else if (currentCommands[0] === 'Put') {
  24.             let word = currentCommands[1];
  25.             let index = +currentCommands[2];
  26.  
  27.             if (questToSolve[index] !== 'undefined') {
  28.  
  29.                 questToSolve.splice(index - 1, 0, word);
  30.             }
  31.  
  32.  
  33.         } else if (currentCommands[0] === 'Replace') {
  34.             let word1 = currentCommands[1];
  35.             let word2 = currentCommands[2];
  36.             let word2Index = questToSolve.indexOf(word2);
  37.             if (word2Index !== -1) {
  38.                 questToSolve[word2Index] = word1;
  39.             }
  40.         } else if (currentCommands[0] === 'Sort') {
  41.             questToSolve.sort((a, b) => b.localeCompare(a))
  42.         }
  43.         currentCommands = arrayInput.shift().split(' ');
  44.     }
  45.     console.log(questToSolve.join(' '));
  46. }
RAW Paste Data