Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input) {
- let string = input.shift();
- let translate = (string, obj, replacement) => {
- let rgx = new RegExp(obj, 'g');
- string = string.replace(rgx, replacement);
- console.log(string);
- return string;
- }
- let includes = (string, command) => {
- if(string.includes(command[1])){
- console.log('True');
- }
- else{
- console.log('False');
- }
- return string;
- }
- let start = (string, command) => {
- if(string.startsWith(command[1])){
- console.log('True');
- }
- else{
- console.log('False');
- }
- return string;
- }
- let lowerCase = (string) => {
- string = string.toLowerCase();
- console.log(string);
- return string;
- }
- let findIndex = (string,command) => {
- console.log(string.lastIndexOf(command[1]));
- return string;
- }
- let remove = (string) => {
- string = string.split('');
- let startIndex = Number(command[1]);
- let count = Number(command[2]);
- string.splice(startIndex, count);
- console.log(string.join(''));
- return string;
- }
- let command ='';
- for (let i = 0; i < input.length-1; i ++) {
- command = input[i].split(' ');
- let obj = command[1];
- let replacement = command[2];
- if (command.includes('Translate')) {
- translate(string, obj, replacement);
- }
- else if(command.includes('Includes')){
- includes(string, command);
- }
- else if(command.includes('Start')) {
- start(string, command);
- }
- else if(command.includes('Lowercase')){
- lowerCase(string);
- }
- else if(command.includes('FindIndex')){
- findIndex(string, command);
- // Find the all indeces where {char} occurs, then print them separated by a space. If no occurences - print "None".
- }
- else if(command.includes('Remove')) {
- remove(string, command);
- }
- }
- return string;
- }
- solve([
- '//Thi5 I5 MY 5trING!//',
- 'Translate 5 s',
- 'Includes string',
- 'Start //This',
- 'Lowercase',
- 'FindIndex i',
- 'Remove 0 10',
- 'End'
- ]
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement