Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. function theFinalQuest(input) {
  2. let sentence = input.shift()
  3. .split(' ');
  4.  
  5. for (let i = 0; i < input.length; i++) {
  6. let tokens = input[i].split(' ');
  7. let command = tokens[0];
  8. let word = tokens[1];
  9. let wordOrIndex = tokens[2];
  10.  
  11. if (command === "Delete") {
  12. let validIndex = Number(word) + 1;
  13. if (validIndex !== -1) {
  14. sentence.splice(validIndex, 1);
  15. }
  16. } if (command === "Swap") {
  17. let temp = word;
  18. if (sentence.includes(word) && sentence.includes(wordOrIndex)) {
  19. let indexOfFirstWord = sentence.indexOf(word);
  20. let indexOfSecondWord = sentence.indexOf(wordOrIndex);
  21. sentence.splice(indexOfFirstWord, 1);
  22. sentence.splice(indexOfFirstWord, 0, wordOrIndex);
  23. sentence.splice(indexOfSecondWord, 1);
  24. sentence.splice(indexOfSecondWord, 0, word);
  25. }
  26. } if (command === "Put") {
  27. let validIndex = Number(wordOrIndex) - 1;
  28. if (validIndex !== -1) {
  29. sentence.splice(validIndex, 0, word);
  30. }
  31. } if (command === "Sort") {
  32. sentence.sort((a, b) => b.localeCompare(a))
  33. } if (command === "Replace") {
  34. if (sentence.includes(wordOrIndex)) {
  35. let indexOfWord = sentence.indexOf(wordOrIndex);
  36. sentence.splice(indexOfWord, 1);
  37. sentence.splice(indexOfWord, 0, word);
  38. }
  39. } if (command === "Stop") {
  40. break;
  41. }
  42. }
  43. console.log(sentence.join(' '))
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement