Phone no: +234 705 673 3798 or email us: info@kdesglobal.com
In this article, we will use three approaches (First with a FOR loop, second using the sort() method, and third using the reduce() method) to Find the Longest Word in a Javascript String.
1. Find the Longest Word With a FOR Loop
We will use the String.prototype.split() method
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: 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”];
Solution
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");
2. Find the Longest Word With the sort() Method
For this solution, we will use the Array.prototype.sort() method to sort the array by some ordering criterion and then return the length of the first element of this array.
In our case, if we just sort the array
var sortArray = [“The”, “quick”, “brown”, “fox”, “jumped”, “over”, “the”, “lazy”, “dog”].sort();
we will have this output:
var sortArray = [“The”, “brown”, “dog”, “fox”, “jumped”, “lazy”, “over”, “quick”, “the”];
In Unicode, numbers come before upper case letters, which come before lower case letters.
We need to sort the elements by some ordering criterion,
[].sort(function(firstElement, secondElement) { return secondElement.length — firstElement.length; })
where the length of the second element is compared to the length of the first element in the array.
function findLongestWord(str) {
var longestWord = str.split(' ').sort(function(a, b) { return b.length - a.length; });
return longestWord[0].length;
}
findLongestWord("The quick brown fox
jumped over the lazy dog");
For this solution, we will use the Array.prototype.reduce().
reduce() executes a callback function once for each element present in the array.
You can provide an initial value as the second argument to reduce, here we will add an empty string “”.
[].reduce(function(previousValue, currentValue) {...}, “”);
function findLongestWord(str) {
var longestWord = str.split(' ').reduce(function(longest, currentWord) {
return currentWord.length > longest.length ? currentWord : longest;
}, "");
return longestWord.length;
}
findLongestWord("The quick brown fox jumped over the lazy dog");
Read Now Top 15 Reasosns why you need a website for your BusinessYou don’t need to do all of these alone, We got you covered!! Contact us now your satisfaction is always our priority. price definitely won't be a problem.
« Previous The Most Common Tricks Hackers Use to Hack Passwords |
Next » Various Ways Your WhatsApp Messages Can get Hacked |
Written by: Idika Destiny
Reading time:
Published 16 hours Ago On Tuesday, September 12, 2023
Updated 16 hours Ago On Tuesday, September 12, 2023
623 Views