Tell us what’s happening:
What’s the problem with my code.
I have noticed that when I console.log(index)
, I get -1
as output. But index is equal to 4
according to the sentence in the last line
Your code so far
function myReplace(str, before, after) {
str = str.split(' ')
let index = str.indexOf(before);
if(before[0] == before[0].toUpperCase()){
after = after[0].toUpperCase() + after.slice(1)
}
str.splice(index, 1, after)
return str.join(' ');
}
console.log(myReplace("A quick brown fox jumped over the lazy dog", "Jumped", "leaped"));
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
.
Challenge: Search and Replace
Link to the challenge: