My solution is right but it tells me it's wrong

So I wrote:

function myReplace(str, before, after) {
  console.log(str.replace(before, after[0].toUpperCase().concat(after.slice(1))))
  return after.charAt(0) === after.charAt(0).toUpperCase() 
  ?
  str.replace(before, after[0].toUpperCase().concat(after.slice(1)))
  :
  str.replace(before, after);

}

myReplace("His name is Tom", "Tom", "john");

The case of the word you’re calling after isn’t automatically capitalized - if the first letter of before is upper case, then the first letter of after should be to…

The case of the replacement should be the same as the replaced word.

1 Like

Right so just have to change it to before, but I still have to account for if after first letter is uppercase and before first letter is not. Can I still do that with ternary operator or do I have to start from scratch?

return before.charAt(0) === before.charAt(0).toUpperCase() ?
  str.replace(before, after[0].toUpperCase().concat(after.slice(1)))
  :
  str.replace(before, after);

Edit: nevermind

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.