Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wordDeveloping(input) {
- let index = 0;
- let string = input[index];
- index++;
- let newString = '';
- while (string !== 'End') {
- let [command, value] = string.split(' ');
- let indexOccurance = false;
- if (command === 'Add') {
- newString += value;
- } else if (command === 'Upgrade') {
- let wordArray = Array.from(newString);
- for (let char of wordArray) {
- if (char === value) {
- let charIndex = wordArray.indexOf(char);
- let code = newString.charCodeAt(charIndex);
- code += 1;
- value = String.fromCharCode(code);
- wordArray.splice(charIndex, 1, value)
- }
- }
- newString = wordArray.join('');
- } else if (command === 'Print') {
- console.log(newString);
- } else if (command === 'Index') {
- let wordArray = Array.from(newString);
- let charIndexes = [];
- for (let i = 0; i < wordArray.length; i++) {
- if (wordArray[i] === value) {
- indexOccurance = true;
- charIndexes.push(i);
- }
- }
- if (indexOccurance) {
- console.log(charIndexes.join(' '));
- } else {
- console.log('None');
- }
- } else if (command === 'Remove') {
- newString = newString.split(value).filter(Boolean).join('');
- }
- string = input[index];
- index++;
- }
- }
- wordDeveloping(["Add University",
- "Print",
- "Upgrade n",
- "Print",
- "Index i",
- "Remove sity",
- "Print",
- "End"]);
- console.log('---');
- wordDeveloping(["Add HelloWorld",
- "Upgrade e",
- "Print",
- "Index b",
- "Remove rl",
- "Print",
- "End"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement