Title Case a Sentence - result ok but still not passed

Tell us what’s happening:

When I am using this piece of code is jsfiddle.net it shows me perfect result. Even in FCC console log its showing correct result for all the testcases but somehow it is not passed, not sure why.

Your code so far

function titleCase(str) {
  
  var words = str.split(' ');
  var ans = "";
  
  for(var i = 0; i < words.length; i++) {
    ans += words[i].charAt(0).toUpperCase() + 
           words[i].substring(1).toLowerCase() + 
           " ";
  }
  
  return ans;
}

titleCase("sHoRt AnD sToUt");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36.

Link to the challenge:

"Short And Stout " is not the correct result, you have an extra space at the end.

1 Like

Thanks a lot!! Fixed it now :slight_smile:

function titleCase(str) {

var words = str.split(’ ');
var ans = ‘’;

for(var i = 0; i < words.length; i++) {
ans += words[i].charAt(0).toUpperCase() + words[i].substring(1).toLowerCase() ;
if (i < words.length - 1)
ans += ’ ';
}

return ans;
}

titleCase(“sHoRt AnD sToUt”);

1 Like