Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The final quest
- function final(input) {
- let sentence = input.shift().split(" ");
- for (let i = 0; i < input.length; i++) {
- let cmd = input[i].split(" ")[0];
- let wordOne = input[i].split(" ")[1];
- let wordTwo = input[i].split(" ")[2];
- if (cmd === "Delete") {
- //Delete {index} – removes the word after the given index if it is valid.
- if (typeof sentence[wordOne] !== "undefined") {
- let index = +wordOne + 1;
- sentence.splice(index, 1);
- }
- } else if (cmd === "Swap") {
- //Swap {word1} {word2} – find the given words in the collections if they exist and swap their places.
- if(sentence.includes(wordOne) && sentence.includes(wordTwo)) {
- let indexOfWordOne = sentence.indexOf(wordOne);
- let indexOfWordTwo = sentence.indexOf(wordTwo);
- sentence.splice(indexOfWordOne, 1, wordTwo);
- sentence.splice(indexOfWordTwo, 1, wordOne);
- }
- } else if (cmd === "Put") {
- //Put {word} {index} – add a word at the previous place {index} before the given one, if it exists.
- if (typeof sentence[wordTwo] !== "undefined") {
- let index = +wordTwo - 1;
- sentence.splice(index, 0, wordOne);
- }
- } else if (cmd === "Sort") {
- //Sort – you must sort the words in descending order
- sentence.sort(function(a, b) {
- return b.localeCompare(a)
- });
- } else if (cmd === "Replace") {
- //Replace {word1} {word2} – find the second word {word2} in the collection (if it exists) and replace it with the first word – {word1}.
- if(sentence.includes(wordTwo)) {
- sentence.splice(sentence.indexOf(wordTwo), 1, wordOne);
- }
- } else {
- break;
- }
- }
- return(sentence.join(" "));
- }
- console.log(final([`Congratulations! You last also through the have challenge!`,
- `Swap have last`,
- `Replace made have`,
- `Delete 2`,
- `Put it 4`,
- `Stop`]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement