Find the Longest Word in a String:
function findLongestWordLength(str) {
return Math.max(...str.split(" ").map(i => i.length));
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
Find the Longest Word in a String:
function findLongestWordLength(str) {
return Math.max(...str.split(" ").map(i => i.length));
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
You could try splitting on space, and then sort the resulting array by length using a lambda:
var input = "The quick brown fox jumped over the lazy dog";
var parts = input.split(" ");
parts.sort((a, b) => a.length < b.length ? 1 : -1);
console.log(parts[0]);
The first element printed should correspond to the word in the original sentence which is the longest. Of course, this answer does not cater to ties, but if we wanted to handle ties, we could add more logic to the lambda use to sort.