Title Case a Sentence won't pass second test

Title Case a Sentence won't pass second test
0.0 0

#1

Tell us what’s happening:
This is my code it works when i try it in other editor but won’t work in FreeCodeCamp interface. It’s accurate enough to pass the second test but won’t.

Can someone give me some insight as to why this is happening?

Your code so far

var output = '';
function titleCase(str) {
  str = str.split(' ').map(function(item){
    item = item.replace(item[0],item[0].toUpperCase());
    newStr.push(item);
   //console.log(newStr);
  });
  output = newStr.join(' ');
  return output;
  
}

titleCase("I'm a little tea pot");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:


#2

The global variable output is the problem. The tests are run one after another, so at each call to the function, output starts with the value it had at the end of the function call. Try moving output initialization inside the function.