There are several syntax errors, and incorrect usage of the Array.forEach
method here. Try the following:
var titleCase = function(txt) {
var words = txt.split(" ");
words.forEach(function(word, idx, array) {
if (word === "the" || word === "and") {
array[idx] = word;
} else {
array[idx] = word.charAt(0).toUpperCase() + word.slice(1);
}
});
return words.join(" ");
};
console.log(titleCase("This is the test"));