Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function theFinalQuest(input) {
- let sentence = input.shift()
- .split(' ');
- for (let i = 0; i < input.length; i++) {
- let tokens = input[i].split(' ');
- let command = tokens[0];
- let word = tokens[1];
- let wordOrIndex = tokens[2];
- if (command === "Delete") {
- let validIndex = Number(word) + 1;
- if (validIndex !== -1) {
- sentence.splice(validIndex, 1);
- }
- } if (command === "Swap") {
- let temp = word;
- if (sentence.includes(word) && sentence.includes(wordOrIndex)) {
- let indexOfFirstWord = sentence.indexOf(word);
- let indexOfSecondWord = sentence.indexOf(wordOrIndex);
- sentence.splice(indexOfFirstWord, 1);
- sentence.splice(indexOfFirstWord, 0, wordOrIndex);
- sentence.splice(indexOfSecondWord, 1);
- sentence.splice(indexOfSecondWord, 0, word);
- }
- } if (command === "Put") {
- let validIndex = Number(wordOrIndex) - 1;
- if (validIndex !== -1) {
- sentence.splice(validIndex, 0, word);
- }
- } if (command === "Sort") {
- sentence.sort((a, b) => b.localeCompare(a))
- } if (command === "Replace") {
- if (sentence.includes(wordOrIndex)) {
- let indexOfWord = sentence.indexOf(wordOrIndex);
- sentence.splice(indexOfWord, 1);
- sentence.splice(indexOfWord, 0, word);
- }
- } if (command === "Stop") {
- break;
- }
- }
- console.log(sentence.join(' '))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement