Find the Longest Word in a String - Basic algorithm scripting

function findLongestWordLength(str) {
var arr = str.split(" ");
var longestWord = [];
for(var i=0; i < arr.length; i++) {
longestWord.push(arr[i].length)
}
return Math.max(…longestWord)
}

findLongestWordLength(“The quick brown fox jumped over the lazy dog”);

Paste your code in chrome console and you might just find out your mistake.

Thanks, it is solved now.

1 Like

Here are a few steps:
-split the string to turn it into an array;
-set a variable to keep the longest to 0;
-use a for to iterate through the new array;
-if the recently created variable is bigger than the current item from the for, simply take its value.