find longest string in string javascript code example
Example 1: find length of longest string in array javascript
const findLongest = words => Math.max(...(words.map(el => el.length)));
// Example
findLongest(['always','look','on','the','bright','side','of','life']); // 6
Example 2: js find longest vword
function findLongestWord(str) {
// Step 1. Split the string into an array of strings
var strSplit = str.split(' ');
// var strSplit = "The quick brown fox jumped over the lazy dog".split(' ');
// var strSplit = ["The", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"];
// Step 2. Sort the elements in the array
var longestWord = strSplit.sort(function(a, b) {
return b.length - a.length;
});
/* Sorting process
a b b.length a.length var longestWord
"The" "quick" 5 3 ["quick", "The"]
"quick" "brown" 5 5 ["quick", "brown", "The"]
"brown" "fox" 3 5 ["quick", "brown", "The", "fox"]
"fox" "jumped" 6 3 ["jumped", quick", "brown", "The", "fox"]
"jumped" "over" 4 6 ["jumped", quick", "brown", "over", "The", "fox"]
"over" "the" 3 4 ["jumped", quick", "brown", "over", "The", "fox", "the"]
"the" "lazy" 4 3 ["jumped", quick", "brown", "over", "lazy", "The", "fox", "the"]
"lazy" "dog" 3 4 ["jumped", quick", "brown", "over", "lazy", "The", "fox", "the", "dog"]
*/
// Step 3. Return the length of the first element of the array
return longestWord[0].length; // var longestWord = ["jumped", "quick", "brown", "over", "lazy", "The", "fox", "the", "dog"];
// longestWord[0]="jumped" => jumped".length => 6
}
findLongestWord("The quick brown fox jumped over the lazy dog");