Find the Longest Word With a FOR Loop

The split() method splits a String object into an array of strings by separating the
string into sub strings.

We will need to add an empty space between the parenthesis of
the split()method,

var strSplit = “The quick brown fox jumped over the lazy
dog”.split(‘ ‘);

which will output an array of separated words:

var strSplit = [“The”, “quick”, “brown”, “fox”, “jumped”,
“over”, “the”, “lazy”, “dog”];

If you don’t add the space in the parenthesis, you will have this output:

var strSplit =
[“T”, “h”, “e”, “ “, “q”, “u”, “i”, “c”, “k”, “ “, “b”,
“r”, “o”, “w”, “n”, “ “, “f”, “o”, “x”, “ “, “j”, “u”, “m”,
“p”, “e”, “d”, “ “, “o”, “v”, “e”, “r”, “ “, “t”, “h”, “e”,
“ “, “l”, “a”, “z”, “y”, “ “, “d”, “o”, “g”];

function findLongestWord(str) {
var strSplit = str.split(' ');
var longestWord = 0;
for(var i = 0; i < strSplit.length; i++){
if(strSplit[i].length > longestWord){
longestWord = strSplit[i].length;
}
}
return longestWord;
}
findLongestWord("The quick brown fox jumped over the lazy
dog");

We need to sort the elements by some ordering criterion. “dog”].split(' ').sort(). which come before lower case letters. “lazy”. if we just sort the array var sortArray = [“The”. “over”. “brown”. Find the Longest Word With the sort() Method For this solution.sort(function(firstElement. In our case. we will use the Array. “the”]. function findLongestWord(str) { // Step 1. . “jumped”. numbers come before upper case letters. “lazy”. “quick”. “brown”.  The sort() method sorts the elements of an array in place and returns the array. [].length. }) *[] is the name of the array where the length of the second element is compared to the length of the first element in the array. “quick”.prototype. In Unicode. “the”. “fox”.length — firstElement. “fox”. we will have this output: var sortArray = [“The”. “jumped”.sort() method to sort the array by some ordering criterion and then return the length of the first element of this array. “dog”. secondElement) { return secondElement. “over”. Split the string into an array of strings var strSplit = str.

"the"] "lazy" "dog" 3 4 ["jumped".sort(function(a. "brown". "over". "over". "brown". // longestWord[0]="jumped" => jumped". // var strSplit = ["The". b) { return b. "dog"].length var longestWord "The" "quick" 5 3 ["quick". "lazy". "The". "lazy". Return the length of the first element of the array return longestWord[0]. "The".length .length.length a.split(' '). }). "the". "The". quick".a. "quick". quick". "fox". "lazy". "The". "over". "brown". "The". // Step 2. "The"] "quick" "brown" 5 5 ["quick". "the". "quick". "fox". // var strSplit = "The quick brown fox jumped over the lazy dog". "brown". "over". "fox".length. quick". // var longestWord = ["jumped". Sort the elements in the array var longestWord = strSplit. "The". quick". "brown". "brown". quick". "fox". "The"] "brown" "fox" 3 5 ["quick". /* Sorting process a b b. "dog"]. "brown". "lazy".length => 6 } findLongestWord("The quick brown fox jumped over the lazy dog"). "over". "The". "the". "dog"] */ // Step 3. . "over". "the"] "the" "lazy" 4 3 ["jumped". "jumped". "fox"] "jumped" "over" 4 6 ["jumped". "brown". "fox". "brown". "fox"] "over" "the" 3 4 ["jumped". "fox"] "fox" "jumped" 6 3 ["jumped".

reduce() executes a callback function once for each element present in the array. currentWord) { return currentWord. .length > longest. *[] is the name of the array function findLongestWord(str) { var longestWord = str. return longestWord.reduce(function(previousValue.reduce(function(longest.split(' '). ""). currentValue) {..length ? currentWord : longest. Find the Longest Word With the reduce() Method The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.}.length. []. } findLongestWord("The quick brown fox jumped over the lazy dog"). here we will add an empty string “”. You can provide an initial value as the second argument to reduce. }.. “”).