Help with Title Case a Sentence problem

Hi! I’ve been trying to capitalize the first letter of each word in a given sentence, but I’m not sure if I’m on the right track. I am not able to filter through the entire array, as it only goes through the first word 4 times. :disappointed_relieved: Any hints would be appreciated!


function titleCase(str) {
  let upperC;
  let splitted = str.split(" ");
  for (let i = 0; i < splitted.length-1; i++) {
    upperC = splitted[0][0].toUpperCase();
    console.log(upperC);
 }
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 YaBrowser/18.9.1.954 Yowser/2.5 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

So your splitted is the array of words, split by space. Inside your loop, you want to get the first letter of the word in the array at index i, but instead you’re getting the first letter of the word in the array at index 0. That happens when you do splitted[0], you’re on the first word. What if, instead of hard-coding a number there, you could use a variable that indicates the index?

Hmm…

// perhaps 

upperC = splitted[someIndexValueHere][0].toUpperCase();

// but what would you use for `someIndexValueHere`? 
// You've already created it, it's right there in your code. ;)

Hi! Thanks for trying to help. :grinning: It’s been days but I still don’t know how to go about it when I try to use slice to remove the first letter, but instead of staying as an array it simply becomes a string of the last array… this only seems to happen when I attach [i] to bananaSplit. :frowning: It’s really frustrating since I feel like I’m getting close, but I don’t know how to approach it next.


function titleCase(str) {
  var bananaSplit = str.split(" ");
  var ultimateArray = [];
  var help;
  var final;
for (var i = 0; i < bananaSplit.length; i++){
ultimateArray.push(bananaSplit[i][0].toUpperCase());
help = bananaSplit[i].slice(1);
// final = ultimateArray.join(" ");
  }
  console.log(help);
}

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

I’m so sooo close… but the code outputs:
I 'm A L ittl T ea P ot
instead of: I’m A Little Tea Pot… xD lol

function titleCase(str) {
  var bananaSplit = str.split(" ");
  var ultimateArray = [];
  var help;
  var arrayJoin = [];
  var final;

  for (var i = 0; i < bananaSplit.length; i++){
    ultimateArray.push(bananaSplit[i][0].toUpperCase());
    help = bananaSplit[i].slice(1,bananaSplit.length);

    arrayJoin.push(help);
    ultimateArray.push(arrayJoin[i]);
    
    final = ultimateArray.join(" ");
 //console.log(arrayJoin);
  }
 console.log(final);
}

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

As you told it to. "Join all array elements with a space between each.

How about push()ing a SINGLE string, inside the loop, concatenated from two smaller ones?

Hi! Thank you so much for taking the time to help me out. :slight_smile: I was stuck so it took a while, but I finally solved without looking at other solutions. The original code:

function titleCase(str) {
  var bananaSplit = str.split(" ");
  var ultimateArray = [];
  var help;
  var arrayJoin = [];
  var final;
  var ripperoni = [];

  for (var i = 0; i < bananaSplit.length; i++){
    ultimateArray.push(bananaSplit[i][0].toUpperCase());
    help = bananaSplit[i].slice(1);
    arrayJoin.push(help.toLowerCase());
    ripperoni.push(ultimateArray[i] + arrayJoin[i]);
  }
  final = ripperoni.join(" ");
  return final;
}

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

After I solved it, I had a programmer look at my code to weed out mistakes and bad habits, and in the end we shortened it to look like this:

function titleCase(str) {
  var bananaSplit = str.split(" ");
  var ripperoni = [];

  for (var i = 0; i < bananaSplit.length; i++){
    var firstLetter = bananaSplit[i][0].toUpperCase();
    var weirdVar = bananaSplit[i].slice(1);
    var otherLetters = weirdVar.toLowerCase();
    ripperoni.push(firstLetter + otherLetters);
  }
  return ripperoni.join(" ");
}

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

Anyway, really appreciate it @snowmonkey!