TitleCase every word in a Sentence

TitleCase every word in a Sentence
0

#1

function titleCase(str) {
var s=str.split(" “);
for(i=0;i>s.length;i++){
if(i==s[i][0]){
s.toUpperCase(i);
}
s= s.join(” ");
return s;

}
}

titleCase(“I’m a little tea pot”);

Were am i going wrong?


#2

You have several problems in your code. See my comments below in your code:

function titleCase(str) {
  var s=str.split(" ");
  for(i=0;i>s.length;i++){ // any code in your for loop will not execute because i will be greater than s.length
    if(i==s[i][0]){ // if you get the for loop condition fixed, you are comparing i (an integer between 0 and s.length) to the first letter of each word in the array.  This is not what you want to compare.
      s.toUpperCase(i);
    }
    s= s.join(" "); // you are changing s from an array to a string, so this will cause trouble in all the above code/logic.  This should be one of the last things done before returning its value back (using return statement - see below)
    return s; // after the first iteration in your for loop, you will return s.  This should be moved outside of the for loop
  }
}

titleCase("I'm a little tea pot");