Intermediate Algorithm Scripting: Search and Replace - Right solution?

Hello! :slight_smile:
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! :slight_smile:

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');

What’s the output are you expecting?

Looks like it’s doing what it’s supposed to do since it passes the challenge test cases.

Hi Phillip,

It’s just that maybe I don’t get how to debug…If I put some console.log() to test what is doing I don’t get the right output…I’m looking for the right way to debug. In fact, when I pasted the code in FCC I was amazed it’s working! :stuck_out_tongue: