Intermediate Algorithm Scripting: Search and Replace - Error?

This code gives the correct output when I review it using console.log but the challenge just refuses to accept this code.
link to the challenge : search and replace

function myReplace(str, before, after) {
  let work=str.split(" ");
  str="";
  for (let i=0;i<work.length;i++)
  {
    if(work[i]==before)
    {
      if(before.charAt(0)==before.charAt(0).toUpperCase())
      {
        after=after.charAt(0).toUpperCase()+after.slice(1);
      }
      work[i]=after;
    }
    str=str+" "+work[i];
  }
  return str;
}

myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped");

The challenge refuses to accept your code because you have an extra space at the beginning. You aren’t returning the requested result so the tests are letting you know that your code doesn’t work as requested.

1 Like

Thanks a lot :+1: :smiley: sorry I did not catch that :sweat_smile:

1 Like