Build a Title Case Converter

I don’t currently want any help attaining the answer to the actual project, but I do want to know why it is displaying and empty array and then an uppercase J, if anyone knows.

function titleCase (title) {
  title = title.toLowerCase();
  title = title.split(" ");
  for (let i = 0; i < title.length; i++) {
    title[i] = title[i].split("");
  }
  let firstLetter = [];
  for (let i = 0; i < title.length; i++) {
    console.log(firstLetter);
    firstLetter.splice(0, 1, title[i][0]);
    firstLetter = firstLetter[0].toUpperCase();
  }
  return title
}

titleCase("Jimmy blah blah blah.");

Console output:

please share a link to the project

console.log(firstLetter);

you have this, those are the values of firstLetter

it start as an empty array let firstLetter = [];

then you change it’s value with firstLetter = firstLetter[0].toUpperCase();

The Project
Yes that is all my code.

also, now that firstLetter is a string, splice gives that error because it’s not a string method

So it's logging it twice from the same- oh it's in a loop.

So .toUpperCase() makes it a string?

no, .toUpperCase() does not turn firstLetter into a string.

what makes it a string is when you assign firstLetter a new value in this line, instead of assigning the value to an element which is stored inside firstLetter :slight_smile:

1 Like