Return String by capitalizing every first letter of the each word and rest in lower case

Tell us what’s happening:
I split the string at ’ ’ and store all the words in in strSplit. The used FORLOOP for capitalizing first letter of every word to capitalize and stored in variable firstLetter. Stored rest of the word in variable restLetter. Then use + to add firstLetter and restLetter and stored this result in newLetter. Now i want to use join to take away “” form each word so that it becomes a one string with every first letter capitalized in each word. but my if i applied join on newLetter it is not working.

Your code so far

function titleCase(str) {
   var strSplit = (str.split(' '));
  var newLetter = [];
  var returnString;
  var firstLetter;
  var restLetter;
   for(i=0; i <= strSplit.length ; i++){
    firstLetter = strSplit[i].charAt(0).toUpperCase();   
    
    //console.log(firstLetter);  
    restLetter = strSplit[i].slice(1, str.Split).toLowerCase();
    
    newLetter = firstLetter + restLetter;

       newLetter.join(" and ");

  }  
    return newLetter;
}
}
//
titleCase("I'm a little tea pot");
  

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/title-case-a-sentence

Your newLetter is a string, not an array.

There are several errors with your code. First of all, you do not nest a loop within the main loop if they iterate over the same array. Secondly, you do not need strSplit.length - 1. Just do strSplit.length. You have the right notion of having first letter and rest letter. Think about replacing the current item in the array with those two variables.

Some one over the stackoverflow yesterday suggested that I should use strSplit.length-1 instead of strSplit.length as arrays start at 0. So i am really confused now.

I changed newLetter to string but it still does not work…

Well, to be honest, it is preference. You can do i <= strSplit.length - 1 or i < strSplit. They are the same thing.

hey! ^^ Hope you’re not still confused, but just in case:

syntax:

  • when you call toLowerCase you forgot (). Just like toUpperCase() it should have parens at the end ^^
    -as you posted your code you seem to have an extra curly brace

and substance ^^:

  • inside your loop you define a newLetter, which is a string of the new word. You then try to join " and " to this string. Join is an array method that doesn’t work on the string. It’s easier if you:
  1. make an empty array on top of your program (fe called returnArray)
  2. in your loop (instead of join), you push() the newLetter unto that array (returnArray.push(newLetter)
  3. and then outside your loop you make returnArray into the string you want by using join() (returnString = returnArray.join(" and ")

like so

function titleCase(str) {
var strSplit = (str.split(’ '));
var newLetter = “”;
var returnString;
var returnArray = [];
var firstLetter;
var restLetter;
for(i=0; i < strSplit.length ; i++){
firstLetter = strSplit[i].charAt(0).toUpperCase();
restLetter = strSplit[i].slice(1).toLowerCase();
newLetter = firstLetter + restLetter;
returnArray.push(newLetter);

}
returnString = returnArray.join(" and ");
return returnString;
}
titleCase(“I’m a little tea pot”);

good luck ^^

Thanks for your help, I had solved it sligthly changing somethings but my solution is very close to yours.

I thought so because it is working the same way.

Thank I ll correct it!!