repl.it
@QuemuelVital/

Search and Replace

Nodejs

Perform a search and replace on the sentence using the arguments provided and return the new sentence.

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function myReplace(str, before, after) {
  let newStr;
  let reg = /[A-Z]/;
  let boo = false;


  if (str.charAt(str.indexOf(before)).match(reg)){
    boo = true;
    newStr = after[0].toUpperCase() + after.substring(1);
    newStr = str.replace(before, newStr);
  }

  if (boo === false){
     newStr = str.replace(before, after);
  }
 
  return newStr;
}

myReplace("Let us go to the store", "store", "mall");

// Try to refactor the first "if" to only capitalize the "after" paramater, and in the outside do the replace for both situations. Goal = exclude the boolean variable.