Advertisement
-Enigmos-

wordDeveloping.js

Apr 11th, 2022
944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function wordDeveloping(input) {
  2.     let index = 0;
  3.     let string = input[index];
  4.     index++;
  5.     let newString = '';
  6.  
  7.     while (string !== 'End') {
  8.         let [command, value] = string.split(' ');
  9.         let indexOccurance = false;
  10.  
  11.         if (command === 'Add') {
  12.             newString += value;
  13.         } else if (command === 'Upgrade') {
  14.             let wordArray = Array.from(newString);
  15.             for (let char of wordArray) {
  16.                 if (char === value) {
  17.                     let charIndex = wordArray.indexOf(char);
  18.                     let code = newString.charCodeAt(charIndex);
  19.                     code += 1;
  20.                     value = String.fromCharCode(code);
  21.                     wordArray.splice(charIndex, 1, value)
  22.                 }
  23.             }
  24.             newString = wordArray.join('');
  25.         } else if (command === 'Print') {
  26.             console.log(newString);
  27.         } else if (command === 'Index') {
  28.             let wordArray = Array.from(newString);
  29.             let charIndexes = [];
  30.             for (let i = 0; i < wordArray.length; i++) {
  31.                 if (wordArray[i] === value) {
  32.                     indexOccurance = true;
  33.                     charIndexes.push(i);
  34.                 }
  35.             }
  36.  
  37.             if (indexOccurance) {
  38.                 console.log(charIndexes.join(' '));
  39.             } else {
  40.                 console.log('None');
  41.             }
  42.         } else if (command === 'Remove') {
  43.             newString = newString.split(value).filter(Boolean).join('');
  44.         }
  45.  
  46.         string = input[index];
  47.         index++;
  48.     }
  49. }
  50.  
  51. wordDeveloping(["Add University",
  52.     "Print",
  53.     "Upgrade n",
  54.     "Print",
  55.     "Index i",
  56.     "Remove sity",
  57.     "Print",
  58.     "End"]);
  59.  
  60. console.log('---');
  61.  
  62. wordDeveloping(["Add HelloWorld",
  63.     "Upgrade e",
  64.     "Print",
  65.     "Index b",
  66.     "Remove rl",
  67.     "Print",
  68.     "End"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement