Title Case A Sentence - using nested for loops

Title Case A Sentence - using nested for loops
0

#1

Tell us what’s happening:
I eventually passed this problem using .map etc… however, on my first attempt the code I used (below) returned the correct results but FCC would not pass the challenge. Just wondering if anyone can see why the code will not pass?

Your code so far

function titleCase(str) {
  
  str = str.toLowerCase();
  var arr = str.split(' ');
  
  var newString = "";
  for(var i = 0; i < arr.length; i++){
    for(var j = 0; j < arr[i].length; j++){
      if(j === 0){
        newString += arr[i][j].toUpperCase();
      } else{
        newString += arr[i][j];
      }
    }
    newString += " ";
  }
  
  return newString;
}

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

Your browser information:

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

Link to the challenge:


#2

The solution you have here adds an extra space character at the end of the returned string.

The following call:

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

returns

'I’m A Little Tea Pot ’

when it should return

'I’m A Little Tea Pot’


#3

Thanks very much! I’ll try to pay more attention to issues like that.