This title-case formatter mangles every other word somehow.
function toTitleCase(sentence) {
const words = sentence.split(' ');
const result = [];
for (let i = 0; i < words.length; i++) {
const word = words[i];
const firstLetter = word.charAt(0).toUpperCase();
const rest = word.slice(1).toLowerCase();
result.push(firstLetter + rest);
}
return result.join(' ');
}
const headlines = [
'the quick brown fox',
'ATTACK OF THE KILLER TOMATOES',
'a tale of two cities'
];
headlines.forEach(h => {
console.log(toTitleCase(h));
});
const nameTag = document.getElementById('preview');
nameTag.textContent = toTitleCase('welcome new employee');
Reply with what is broken and how you would fix it.