I’m trying to write a function that takes a string and capitalizes the first letter in each word that is not included in the “minorWords” string. What is missing in my code that causes the return value to be “undefined”? After writing this function a few different ways I am now thinking that I am just using .forEach incorrectly. I am fairly certain that I am using the ternary operator appropriately, but I tried substituting an if statement and got the same result ( undefined ). I am also not sure why undefined is being returned twice . . .
function titleCase1(title, minorWords) {
var titleArray = title.split(" ");
var minorArray = minorWords.split(" ");
var newArray = titleArray.forEach(function(word){
(word in minorArray)? word :
word[0].toUpperCase() + word.slice(1);
})
console.log(newArray);
}
titleCase1("the wind in the willows", "the in");
// -> undefined undefined
I realize that if this works the first "the" will not be capitalized, but I will figure that out once I am no longer misusing the tools I have here . . .