Title Case a Sentence challenge help

Title Case a Sentence challenge help
0

#1

Hello. I’m having issues with the Title Case a Sentence challenge. I’ve tried many “solutions” and none of them worked for me. Can you find the culprits in my code?

 function titleCase(str) {
          var lowercasedStr = str.toLowerCase();
          var stringByWords = lowercasedStr.split(/\s/);
          var finalArray = [];
          for (var i = 0; i < stringByWords.length; i++) {
            finalArray = stringByWords.map(stringByWords[i].charAt(0).toUpperCase());
          }
          return finalArray.join(" ");
        }
    titleCase("I'm a little tea pot");

Thanks in advance.


#2

Debugger says:
TypeError: stringByWords[i].charAt(…).toUpperCase(…) is not a function

And in any case doing this in loop
finalArray = stringByWords.map(stringByWords[i].charAt(0).toUpperCase());
will just attempt to assign one last loop value to all array, to update array at each loop iteration you need to use another way.


#3

why are you using a for loop and map at the same time. take away the for loop and just use map instead.

return stringByWords.map(function(val){
return val[0].toUpperCase + val.substr(1);
});
.join(" ");