[SOLVED] Title Case a Sentence - good result bad compiler

Tell us what’s happening:

My code returns exactly as requested but for the compiler it does not work. Can you tell me what the problem is?

Your code so far

function titleCase(str) {
    str = str.toLowerCase();
    var array1 = str.split(' '); 
    var str1="";
    console.log(array1);
    for(i=0; i<array1.length; i++) {
        var array2 = array1[i];
        str1 += array2[0].toUpperCase();
        for(j=1; j<array2.length;j++) {
            str1 += array2[j];

        }str1 +=" ";
    }

    console.log(str1);
    str = str1;
    return str;
}

titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");


Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/title-case-a-sentence

It’s not obvious, but the strings you’re returning have one space at the end.

You’re right, thanks!

I changed my code and now it’s works!

This is the new code:

function titleCase(str) {

str = str.toLowerCase();
var array1 = str.split(’ ');
var str1="";
console.log(array1);
for(i=0; i<array1.length; i++) {
var array2 = array1[i];
str1 += array2[0].toUpperCase();
for(j=1; j<array2.length;j++) {
str1 += array2[j];

    } if(i != array1.length-1) {str1 +=" ";} \\new line for avoid space at the end of sentence 
}

return str1;

}

titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”);