Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let text = '';
- const add = (string) => {
- text = text.concat(string);
- };
- const upgrade = (char) => {
- let incrementedChar = String.fromCharCode(char.charCodeAt(0) + 1);
- let pattern = new RegExp(char, "g");
- text = text.replace(pattern, incrementedChar);
- };
- const print = () => {
- console.log(text);
- };
- const findIndex = (char) => {
- let indexes = [];
- for (let i = 0; i < text.length; i++) {
- let currentChar = text[i];
- if (currentChar === char) {
- indexes.push(i);
- }
- }
- indexes.length === 0
- ? console.log("None")
- : console.log(indexes.join(' '));
- };
- const remove = (string) => {
- let pattern = new RegExp(string, "g");
- text = text.replace(pattern, '');
- }
- let commandParser = {
- 'Add': add,
- 'Upgrade': upgrade,
- 'Print': print,
- 'Index': findIndex,
- 'Remove': remove
- };
- for (let line of input) {
- if (line === 'End') {
- break;
- }
- let tokens = line.split(' ');
- let command = tokens.shift();
- commandParser[command](...tokens);
- }
- }
- /*solve(['Add abracadabra',
- 'Print',
- 'Upgrade a',
- 'Print',
- 'Index b',
- 'Remove bbrb',
- 'Print',
- 'End'
- ])*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement