Title Case a Sentence, cant get it to pass

Title Case a Sentence, cant get it to pass
0

#1

This is my code so far.

function titleCase(str) {
  var array = str.toLowerCase().split(" ");
  var new_array = "";
  var newword = "";
  for (let i = 0; i < str.length; i++) {
    newword = array[i][0].toUpperCase() + array[i].substring(1);
    new_array += " " + newword.slice();
  }
  return new_array;
}

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

The console.log returns the correct string. However everytime I try and submit my answer, I am told the “Cannot read property ‘0’ of undefined”.


#2

Look at what you are comparing with.

You are actively working with array but you are going off of str instead.

Your array is not as long as your str so it’s trying to grab non existing letters.


#3

What @shimphillip said!

Also, look at what you’re doing with your string building: On the every pass, you add " " and the current word. So what happens on the FIRST word?


#4

I am an idiot. Thanks for your help!


#5

Nope. Not even a LITTLE bit of an idiot. Just, its easy to lose sight of things. Fresh eyes and all that.