Advertisement
Lulunga

mid exam 03. The Final Quest

Jun 29th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  function solve(input) {
  2.                 let words = input.shift().split(' ');
  3.                 for (let line of input) {
  4.                     if (line === 'Stop') {
  5.                         break;
  6.                     }
  7.                     let tokens = line.split(' ');
  8.                     let command = tokens[0];
  9.                     if (command === 'Delete') {
  10.                         let deleteIndex = Number(tokens[1]);
  11.                         deleteWord(deleteIndex);
  12.                     } else if (command === 'Swap') {
  13.                         let first = tokens[1];
  14.                         let second = tokens[2];
  15.                         swap(first, second);
  16.                     } else if (command === 'Put') {
  17.                         let myword = tokens[1];
  18.                         let index = Number(tokens[2]);
  19.                         put(myword, index);
  20.                     } else if (command === 'Sort') {
  21.                         words.sort(sortByDescending);
  22.                     } else {
  23.                         let first = tokens[1];
  24.                         let second = tokens[2];
  25.                         replace(first, second);
  26.                     }
  27.                 }
  28.                 console.log(words.join(' '));
  29.  
  30.                 function deleteWord(index) {
  31.                     index = index + 1;
  32.                     if (index >= 0 && index < words.length) {
  33.                         words.splice(index, 1);
  34.                     }
  35.                 }
  36.  
  37.                 function swap(word1, word2) {
  38.                     let indexOfFirst = words.indexOf(word1);
  39.                     let indexOfSecond = words.indexOf(word2);
  40.  
  41.                     if (indexOfFirst !== -1 && indexOfSecond !== -1) {
  42.                         words[indexOfFirst] = word2;
  43.                         words[indexOfSecond] = word1;
  44.                     }
  45.                 }
  46.  
  47.                 function put(word, index) {
  48.                     index--;
  49.                     if (index >= 0 && index <= words.length) {
  50.                         words.splice(index, 0, word);
  51.                     }
  52.                 }
  53.  
  54.                 function sortByDescending(a, b) {
  55.                     return b.localeCompare(a);
  56.                 }
  57.  
  58.                 function replace(word1, word2) {
  59.                     let secondWordIndex = words.indexOf(word2);
  60.                     if (secondWordIndex >= 0 && secondWordIndex < words.length) {
  61.                         words[secondWordIndex] = word1;
  62.                     }
  63.                 }
  64.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement