function titleCase(str) {var complete ="";
var str2= str.toLowerCase().split(" ");
var str3 = str2;
for (var i =0;i<str3.length;i++){ complete +=str2[i].charAt(0).toUpperCase()+str2[i].substr(1)+" ";}
return complete ;
}
titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
Why it is not working ?
Because you are adding a space on the end.
You are returning
'Here Is My Handle Here Is My Spout '
instead of
'Here Is My Handle Here Is My Spout'
Just return complete.slice(0,complete.length-1).
Also, you do not need str3. Get rid of the var str3=str2 and the for statement would look like:
for (var i =0;i<str3.length;i++){ complete +=str2[i].charAt(0).toUpperCase()+str2[i].substr(1)+" ";}
Actually, if you are going to use the split function as you have, you can simply use the following and get rid of the complete variable and do something like below:
function titleCase(str) {
var str2= str.toLowerCase().split(" ");
for (var i =0;i<str2.length;i++){ str2[i] = str2[i][0].toUpperCase()+str2[i].slice(1);}
return str2.join(" ");
}
I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.
Sorry I shouldn’t have uploaded the code.but thnx all of u to help me out I was really stucked mo