I am posting my solution to the problem to get some feedback. I tried to solve it with as few lines as possible. Is there anyway I could make it even more compact or would that make the code too unreadable?
function findLongestWord(str) {
var wordArray = str.split(" ").sort(function(a,b){return(a.length-b.length);});
return(wordArray[wordArray.length -1].length);
function findLongestWord(str) {
var max,
newArr = [];
var arr = str.split(' ');
for(i = 0; i < arr.length; i++) {
newArr.push(arr[i].length);
}
return newArr.reduce(function(a,b) {
return Math.max(a,b);
});
}
findLongestWord("The quick brown fox jumped over the lazy dog");