JS longest word
function findLongestWordLength(str) {
let words = str.split(' ');
let maxLength = 0;
for (let i = 0; i < words.length; i++) {
if (words[i].length > maxLength) {
maxLength = words[i].length;
}
}
return maxLength;
}
findLongestWordLength("The quick brown fox jumped over the lazy dog");
// 6
4
5
const wordsArray = ['1', '22', '222', 'longstring', '2222']
const longestWord = wordsArray.reduce((longestWord, currentWord) =>
currentWord.length > longestWord.length ? currentWord: longestWord, '');
console.debug(longestWord); // longstring
Thank you!
5
0
3.8
10
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");
Thank you!
10
0
4.25
4
function data(str){
var show = str.split(" ");
show.sort(function (a,b){
return b.length - a.length;
})
return show[0];
}
console.log(data(str = "javascript is my favourite language "));
Thank you!
4
0
4.4
5
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");
Thank you!
5
0
3
1
function findLongestWordLength(str) {
return Math.max(...str.split(' ').map(word => word.length));
}
Thank you!
1
0
Are there any code examples left?
New code examples in category Javascript
-
Javascript 2023-04-21 22:52:36
-
Javascript 2022-03-27 22:25:20 javascript download string as file
-
Javascript 2022-03-27 21:40:22 sort numbers in array javascript
-
Javascript 2022-03-27 21:20:04 compare two arrays and return the difference javascript
-
Javascript 2022-03-27 21:15:02 javascript regex french phone number
-
Javascript 2022-03-27 21:05:03 cypress custom error message
-
Javascript 2022-03-27 21:00:05 create element javascript with id
-
Javascript 2022-03-27 20:40:57 feather icons react
-
Javascript 2022-03-27 20:20:01 how to make graphql request in axios
-
Javascript 2022-03-27 20:15:07 bootstrap validator password and confirm password