The problem is that the lines in javascript are immutable. You cannot just change char as follows.
The solution would be:
words[i] = words[i][0].toUpperCase()+words[i].slice(1);
But you can have simpler and faster code using a regular expression:
return input.replace(/\b\w/g,function(b){ return b.toUpperCase() })
(here with a more complete uppercase, not just after spaces - if you want to use spaces, use replace(/(\s+|^)\w/g,function(b){ return b.toUpperCase() }))
source
share