Issue capitalizing letters while keeping other lowercase

Issue capitalizing letters while keeping other lowercase
0

Tell us what’s happening:
I’m supposed to turn the first letter of every word in a string capital and all the others lowercase.
Not sure why the tests are failing, console logging the newStr gives me the correct output .

Your code so far


function titleCase(str) {
  var arr = str.split(" ")
  var newStr = "";
  for (let i = 0; i < arr.length; i++) {
    let noLetter = arr[i].slice(1).toLowerCase() + " ";
    let upperCase = arr[i].charAt(0).toUpperCase();
    newStr += upperCase + noLetter;
  }
  return newStr;
}

Your browser information:

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

Challenge: Title Case a Sentence

Link to the challenge:

do you think it would be more clear and easier to use the charAt() string method !!

I already use the charAt() method to turn them capital, is that what you mean?

Think about what happens with your code on the last iteration.

Tell us what newStr equals

If you console.log it, newStr is equals to the letters after the first letter in every word so ex:
I'm A Tea Pot
newString = 'm ea ot

Oh, I think its cause I’m adding a blank " " at the end of newStr?

@Catalactics

You’re mistaken. He has everything right except for what I hinted.

@brianjpow
That’s all you were missing.

What some people do is push their results into an array and join by the character they split on. That way you wouldnt have to worry about the end.

1 Like

Got it, awsome, thank you!!