Advertisement
Lulunga

demo final String Manipulator Command parser regex var

Jul 28th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input) {
  2.     let text = '';
  3.  
  4.     const add = (string) => {
  5.         text = text.concat(string);
  6.     };
  7.  
  8.     const upgrade = (char) => {
  9.         let incrementedChar = String.fromCharCode(char.charCodeAt(0) + 1);
  10.         let pattern = new RegExp(char, "g");
  11.         text = text.replace(pattern, incrementedChar);
  12.     };
  13.  
  14.     const print = () => {
  15.         console.log(text);
  16.     };
  17.  
  18.     const findIndex = (char) => {
  19.         let indexes = [];
  20.         for (let i = 0; i < text.length; i++) {
  21.             let currentChar = text[i];
  22.             if (currentChar === char) {
  23.                 indexes.push(i);
  24.             }
  25.         }
  26.         indexes.length === 0
  27.             ? console.log("None")
  28.             : console.log(indexes.join(' '));
  29.     };
  30.  
  31.     const remove = (string) => {
  32.         let pattern = new RegExp(string, "g");
  33.         text = text.replace(pattern, '');
  34.     }
  35.  
  36.     let commandParser = {
  37.         'Add': add,
  38.         'Upgrade': upgrade,
  39.         'Print': print,
  40.         'Index': findIndex,
  41.         'Remove': remove
  42.     };
  43.  
  44.     for (let line of input) {
  45.         if (line === 'End') {
  46.             break;
  47.         }
  48.         let tokens = line.split(' ');
  49.         let command = tokens.shift();
  50.  
  51.         commandParser[command](...tokens);
  52.     }
  53. }
  54.  
  55. /*solve(['Add abracadabra',
  56.     'Print',
  57.     'Upgrade a',
  58.     'Print',
  59.     'Index b',
  60.     'Remove bbrb',
  61.     'Print',
  62.     'End'
  63. ])*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement