This solution works in the Chrome console and in the Repl.it console, but doesn't work in the FCC challenge console. Why not? [SOLVED]

This intermediate Algorithm challenge is fairly simple. It’s called Search and Replace.
Description:

Perform a search and replace on the sentence using the arguments provided and return the new sentence.

First argument is the sentence to perform the search and replace on.

Second argument is the word that you will be replacing (before).

Third argument is what you will be replacing the second argument with (after).

NOTE: Preserve the case of 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”.

It’s not passing tests that have a capitalized before.

function myReplace(str, before, after) {
  
  var titleCaseWord = function(x) {
    x.toUpperCase();
  };
  
  var titleCaseFirstLetter = function(y) {
    y.replace(y[0], titleCaseWord);
  };
  
  if ( before.charCodeAt(0) < 91 ) {
    after = titleCaseFirstLetter(after);
    }
  
  var str2 = str.replace(before, after);
  
  
  return str2;

}

myReplace("He is Sleeping on the couch", "Sleeping", "sitting");

It doesn’t work on all the test cases.

It worked just fine with arrow functions. I replaced arrow functions with standard ones on FCC. Weird.

Try:

function myReplace(str, before, after) {
  var titleCaseWord = (x) => x.toUpperCase();
  var titleCaseFirstLetter = (y) => y.replace(y[0], titleCaseWord);
  
  if ( before.charCodeAt(0) < 91 ) {
    after = titleCaseFirstLetter(after);
    }
  var str2 = str.replace(before, after);
  
  
  return str2;
}


myReplace("He is Sleeping on the couch", "Sleeping", "sitting");

Oh brother… I forgot return in my functions when converting them.
I goofed :tired_face:

1 Like