Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solveTheRiddle(arrayInput) {
- const questToSolve = arrayInput.shift().split(' ');
- let currentCommands = arrayInput.shift().split(' ');
- while (currentCommands[0] !== 'Stop') {
- if (currentCommands[0] === 'Delete') {
- const index = +currentCommands[1];
- if (questToSolve.indexOf(questToSolve[index]) !== -1) {
- questToSolve.splice(index + 1, 1);
- }
- } else if (currentCommands[0] === 'Swap') {
- let word1 = currentCommands[1];
- let word2 = currentCommands[2];
- if (questToSolve.indexOf(word1) && questToSolve.indexOf(word2)) {
- const word1Index = questToSolve.indexOf(word1);
- const word2Index = questToSolve.indexOf(word2);
- const savedFirst = questToSolve[word1Index];
- questToSolve[word1Index] = word2;
- questToSolve[word2Index] = word1;
- }
- } else if (currentCommands[0] === 'Put') {
- let word = currentCommands[1];
- let index = +currentCommands[2];
- if (questToSolve[index] !== 'undefined') {
- questToSolve.splice(index - 1, 0, word);
- }
- } else if (currentCommands[0] === 'Replace') {
- let word1 = currentCommands[1];
- let word2 = currentCommands[2];
- let word2Index = questToSolve.indexOf(word2);
- if (word2Index !== -1) {
- questToSolve[word2Index] = word1;
- }
- } else if (currentCommands[0] === 'Sort') {
- questToSolve.sort((a, b) => b.localeCompare(a))
- }
- currentCommands = arrayInput.shift().split(' ');
- }
- console.log(questToSolve.join(' '));
- }
Add Comment
Please, Sign In to add comment