I have a function that should capitalize every first letter in each word in a string, but somehow it delivers incorrect result, any idea why? I need to fix it a bit.
So the input: hello dolly output: Hello Dolly.
Spaces are correctly counted but the capitalization is incorrect.
function letterCapitalize(str) {
  str = str.replace(str.charAt(0), str.charAt(0).toUpperCase());
  let spaces = [];
  for (let i = 0; i < str.length; i++) {
    if (str[i] === ' ') spaces.push(i);
  }
  for (let space of spaces) {
    str = str.replace(str.charAt(space + 1), str.charAt(space + 1).toUpperCase());
  }
  return str;
}
console.log(letterCapitalize("hello there, how are you?")); 
     
     
    