Find the Longest Word in a String - Basic algorithm scripting

Find the Longest Word in a String - Basic algorithm scripting
0

#1

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”);


#2

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


#3

Thanks, it is solved now.


#4

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.