# Untitled

a guest
Jun 27th, 2019
113
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] + 1;
10.             if (index >= 0 && index < questToSolve.length)  {
11.                 questToSolve.splice(index, 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] = savedFirst;
22.             }
23.         } else if (currentCommands[0] === 'Put') {
24.             let word = currentCommands[1];
25.             let index = +currentCommands[2] - 1;
26.
27.             if (index >= 0 && index <= questToSolve.length) {
28.                 questToSolve.splice(index, 0, word);
29.             }
30.
31.
32.         } else if (currentCommands[0] === 'Replace') {
33.             let word1 = currentCommands[1];
34.             let word2 = currentCommands[2];
35.             let word2Index = questToSolve.indexOf(word2);
36.             if (word2Index !== -1) {
37.                 questToSolve[word2Index] = word1;
38.             }
39.         } else if (currentCommands[0] === 'Sort') {
40.             questToSolve.sort((a, b) => b.localeCompare(a))
41.         }
42.         currentCommands = arrayInput.shift().split(' ');
43.     }
44.     console.log(questToSolve.join(' '));
45. }
46. solveTheRiddle(['This the my quest! final',
47.     'Put is 2',
48.     'Swap final quest!',
49.     'Delete 2',
50.     'Stop',
51.     ''])
RAW Paste Data