Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word “Book” with the word “dog”, it should be replaced as “Dog”.
But in test case there is,
myReplace("Let us go to the store", "store", "mall") should return “Let us go to the mall”.
and also,
myReplace("His name is Tom", "Tom", "john") should return “His name is John”.
I wrote code to change all after values from myReplace function to capitalize.
Why mall should not return Mall but, john should return John?
Note:
Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word “Book” with the word “dog”, it should be replaced as “Dog”.
You are looping through the characters in the string and examine what you are checking in the the if condition. Don’t you see a problem there?
2. And var next = (after[0].toUpperCase()).concat(after.slice(1));. What are you doing there? Don’t you see a problem there too. I suggest you read the task again.
The instruction didn’t ask you to capitalize all the time. What it wants you to do is for example if before is Dog and after is cat, then change cat to Cat however if before is dog and after is cat, then you don’t change anything. Similarly if before is dog and after is Cat, then change Cat to cat. Instead of what you did, I would have gone with something like:
const firstChar = before.charAt(0);
let next;
if(firstChar.toUpperCase() === firstChar ){
next = after[0].toUpperCase() + after.slice(1);
}else{
next = after[0].toLowerCase() + after.slice(1);
}