Problem with Title Case a Sentence

Problem with Title Case a Sentence
0

#1

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”);


#2

When I ran it through http://pythontutor.com/javascript.html#mode=edit
it had a space character at the end of the sentence.


#3

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”);