Problem with Title Case a Sentence

Hi! first of all, thanks for reading .

It’s really a simple question, I made a code for this problem that do the work, but the page doesn’t recognize it as true.

so I want to know your opinions so I can see what’s the trouble.

here’s the code:

‘’'function titleCase(str) {
var result="";
var Copy=str.toLowerCase().split(" ");
var i=0;

while (i<Copy.length){

var Upper= Copy[i][0].toUpperCase();

result +=  Upper+Copy[i].slice(1)+" ";
  if (i==Copy.length){
    result +=  Upper+Copy[i].slice(1);
  }
i++;  

}
return result.replace(/[!"#$%&/()=]/gi,"");

}

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

When I ran it through Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code
it had a space character at the end of the sentence.

Thanks! I though that the if statement would avoid that, I will change it. :wink:

------------------------------- Updated--------------------------------------

I fix the code and it works, now it look like this:

function titleCase(str) {
var result="";
var Copy=str.toLowerCase().split(" ");
var i=0;

while (i<Copy.length){

var Upper= Copy[i][0].toUpperCase();

if (i<(Copy.length-1)){
result += Upper+Copy[i].slice(1)+" ";

i++;
}

else{
result +=  Upper+Copy[i].slice(1);
i++; 
}

}
return result;

}

titleCase(“sHoRt AnD sUUUUToUt”);