Take a closer look at if clauses in the function and try to check whether they behave as expected. You can also follow one of the troublesome examples by hand.
i know it’s kinda messy but it’s working, but now it doesn’t pass the " myReplace("I think we should look up there", "up", "Down") should return “I think we should look down there”." part
function myReplace(str, before, after) {
let sentence = str.split(" ");
for(var i = 0; i < sentence.length; i++){
if(sentence[i] == before && sentence[i].charAt(0) == before.charAt(0).toUpperCase()){
sentence[i] = sentence[i] = after.charAt(0).toUpperCase() + after.slice(1);
}else{
if(sentence[i] == before){
sentence[i] = after;
}
}
}
console.log(sentence);
return sentence.join(" ");
}
let result=myReplace("He is Sleeping on the couch", "Sleeping", "sitting");
Notice that right now character case of after is explicitly matched/changed only if word in original string is uppercase. In other cases the after is used as is.