Why does the first block of code work (with for loop) and the second doesn't (with forEach)?
(I am trying to make all words start with an uppercase letter in a string)
1)
function capitalize(str){
let wordList = str.split(" ");
for (i = 0; i < wordList.length; i++){
wordList[i] = wordList[i][0].toUpperCase() + wordList[i].substring(1);
};
return wordList.join(' ');
};
let str = "How are you doing today?";
console.log(capitalize(str));
2)
function capitalize(str){
let wordList = str.split(" ");
wordList.forEach(function(word){
word = word[0].toUpperCase() + word.substring(1);
})
return wordList.join(' ');
};
let str = "How are you doing today?";
console.log(capitalize(str));