I am brand new to javascript and have only an entry level java college course under my belt. I am taking a course online to learn JS and have run into a hiccup. I am supposed to create a function that parses a string and finds the longest word. I figured the problem out, but could not understand why the following function was giving me an undefined. Any help would be great as I try to deeper my understanding of the language.
var statement = 'This is an example string'
//undefined
var result = statement.split(' ').reduce(function(acc, cur) {
if (acc.length > cur.length) {
return acc;
} else {
return cur;
}
});
console.log(result); //"example"
function longest(string) {
string.split(' ').reduce(function(acc, cur) {
if (acc.length > cur.length) {
return acc;
} else {
return cur;
}
});
}
result = longest(statement);
console.log(result); //undefined