https://www.freecodecamp.com/challenges/title-case-a-sentence#?solution=%0A%0Afunction%20titleCase(str)%20%7B%0A%20%20var%20arryStr%3Dstr.split('%20')%3B%0A%20%20str%3D%22%22%3B%0A%20%20for%20(i%3D0%3B%20i%3CarryStr.length%3Bi%2B%2B)%7B%0A%20%20%20%20var%20temp%20%3DarryStr%5Bi%5D.charAt().toUpperCase()%3B%0A%20%20%20%20var%20lower%3D%22%22%3B%0A%20%20%20%20%20for(j%3D1%3Bj%3CarryStr%5Bi%5D.length%3B%20j%2B%2B)%7B%0A%20%20%20%20%20%20%20lower%2B%3DarryStr%5Bi%5D.charAt(j).toLowerCase()%3B%0A%20%20%20%20%20%20%20%0A%20%20%20%20%20%7D%0A%20%20%20%20str%2B%3Dtemp%2Blower%2B%22%20%22%3B%0A%20%20%7D%0A%20%20%0A%20%20return%20str%3B%0A%7D%0A%0AtitleCase(%22I'm%20a%20little%20tea%20pot%22)%3B%0A
the program I wrote works fine but it will not let me pass
function titleCase(str) {
var arryStr=str.split(' ');
str="";
for (i=0; i<arryStr.length;i++){
var temp =arryStr[i].charAt().toUpperCase();
var lower="";
for(j=1;j<arryStr[i].length; j++){
lower+=arryStr[i].charAt(j).toLowerCase();
}
str+=temp+lower+" ";
}
return str;
}
I think the line str+=temp+lower+" ";
add a whitespace to the end of the text when in the last iteration which makes the strings not match. Hint: Have a look at Array.prototype.join() to avoid the problem.
@cjoshmartin as @pnahratow suggests, double check the spaces you add when you join strings. If you get stuck or have questions with a challenge in the future, please first ask questions to the Help Room. Happy coding!
Most helpful comment
I think the line
str+=temp+lower+" ";
add a whitespace to the end of the text when in the last iteration which makes the strings not match. Hint: Have a look at Array.prototype.join() to avoid the problem.