Hello!
I was trying this solution in VScode with Quokka to preview my code and I was using console.log here and there. For me, in that environment, the code was not outputing what I wanted. But if I copy paste the solution in FCC it works! Can someone tell me what’s wrong or if I’m doing something wrong using this kind of debugging solution?
thanks!
function myReplace(str, before, after) {
//Check if the word "before" exists in the string and if the first letter is uppercase.
if (before.charAt(0) === before.charAt(0).toUpperCase() && str.indexOf(before) != -1) {
//Change "after" word to have the first letter in uppercase.
let newAfter = after.charAt(0).toUpperCase() + after.slice(1);
//Return the string swapping "before" word with the "after" one capitalizing the first letter.
return str.replace(before, newAfter);
} else {
//Check if the word exists as it is.
if (str.indexOf(before) != -1) {
//Return the string swapping "before" word with the "after" one.
return str.replace(before, after);
}
}
}
//Test cases:
myReplace('A quick brown fox jumped over the lazy dog', 'Jumped', 'leaped');
myReplace('He is Sleeping on the couch', 'Sleeping', 'sitting');
myReplace('This has a spellngi error', 'spellngi', 'spelling');
myReplace('His name is Tom', 'Tom', 'john');