toLowerCase, toUpperCase() query

Tell us what’s happening:
Describe your issue in detail here.
I feel like my logic would work, but I seem to have a problem with timing and assignment in my code. I’ve console.logged things and they do what they’re supposed to (the toUpperCase() turns all first letter in a word to capitals, toLowerCase() takes care of all errant capital letters).

So what am I overlooking?

As always, thank you for your help

  **Your code so far**

function titleCase(str) {
let chars = str.toLowerCase();;
str = chars.split(' ');
for(let i = 0; i < str.length; i++){
    str[i][0].toUpperCase();
}
 str.join(' ');
return str;
}

titleCase("I'm a little tea pot");
  **Your browser information:**

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

Challenge: Title Case a Sentence

Link to the challenge:

toUpperCase() turns all first letter in a word to capitals

No, that method it *returns a new string with all the letters in the string converted to uppercase.

There are some other issues with your code.

str[i][0].toUpperCase();

There are two issues here. First, str[i] is a string. You seem to want to change the first char of that string. You cannot do that in JS. You cannot change strings, you can only create a new one and replace the old one. There are languages where you can manipulate individual chars of a string - JS is not one of them.

Also, as alluded to before, toUpperCase does not act on the string itself but returns a new string. So this line takes a string that is the first char of the array’s string, converts it to uppercase, and then does nothing with it - you’d have to save it into a variable to do anything with it.


  str.join(' ');
  return str;

The join method does not change the value of str - again, it returns a new string and str remains unchanged. You need to do something with that return value.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.