Tell us what’s happening:
my solution will work with all the tests having to do with capital letters but won’t work for my else statement dealing with replacement words without capital letters in the 0 index. Can anyone see why it always executes the if statement?
Your code so far
function myReplace(str, before, after) {
let answer = str;
let regex1 = before;
let regex2 = after;
//let regex3 = /[A-Z]/;
// let indexStart = str.indexOf(regex1);
let capital = before.charAt(0);
console.log(capital);
if (capital==='A'||'B'||'C'||'D'||'E'||'F'||'G'||'H'||'I'||'J'||'K'||'L'||'M'||'N'||'O'||'P'||'Q'||'R'||'S'||'T'||'U'||'V'||'W'||'X'||'Y'||'Z'){
let cap = regex2[0].toUpperCase();
let nonCap = regex2.slice(1);
return answer.replace(regex1, cap + nonCap);
} else
return answer.replace(regex1, after);
}
myReplace("A quick brown fox Jumped over the lazy dog", "Jumped", "leaped");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.
if you want a quick way to compare you can use a regex statement. There are many ways to do comparisons. (it’s just that your if statement is not phrased correctly and doesn’t do comparisons)