最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find the longest wordstring in an array - Stack Overflow

programmeradmin3浏览0评论

I just started learning JavaScript. I am trying to write a JavaScript to find and print the longest word in an Array. I came up with the code below:

var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"]
var longest = 0;
for (var i = 0; i < longWords.length; i++) {
if (longest < longWords[i].length) {
    longest = longWords[i];
  }
}

console.log(longest)

The problem is it always end up printing the first element in the array. which means longest = longWords[0]. Now when I change the line longest = longWords[i] to longest = longWords[i].length I get the count of the longest character. Please tell me why it didn't work and how I can do this using the for loop.

I just started learning JavaScript. I am trying to write a JavaScript to find and print the longest word in an Array. I came up with the code below:

var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"]
var longest = 0;
for (var i = 0; i < longWords.length; i++) {
if (longest < longWords[i].length) {
    longest = longWords[i];
  }
}

console.log(longest)

The problem is it always end up printing the first element in the array. which means longest = longWords[0]. Now when I change the line longest = longWords[i] to longest = longWords[i].length I get the count of the longest character. Please tell me why it didn't work and how I can do this using the for loop.

Share Improve this question asked Nov 14, 2013 at 20:10 lmissionlmission 351 gold badge1 silver badge5 bronze badges 1
  • 2 Do you need to handle the use case where two words are of equal length, and are both the longest? – James Mishra Commented Nov 14, 2013 at 20:12
Add a ment  | 

6 Answers 6

Reset to default 3
if (longest < longWords[i].length) {

Should probably be

if (longest.length < longWords[i].length) {

You can custom sort based on string length, and grab the first item:

longWords.sort(function(a, b) { 
    return b.length - a.length; 
});

This turns your array into the following:

["Czechoslovakia", "Aubumayang", "Penelope", "Slovenia", "Johny"]

At that point, you can grab the first item. Note however that there may be other strings immediately following the first that are of the same length.

As for your above code, longest is declared as a number, but later set to a string. The number we're interested in es from the length of the string. Our condition should be:

// No sense in looking this up twice
current = longWord[i];

if ( longest.length < current.length ) {
    longest = current;
}

Rather than finding the longest word, I'd suggest sorting the array by descending length of its elements, using Array.prototype.sort():

var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"],
    sorted = longWords.sort(function (a, b) {
    return a.length < b.length;
});

console.log(sorted);
// ["Czechoslovakia", "Aubumayang", "Penelope", "Slovenia", "Johny"]

JS Fiddle demo.

References:

  • Array.sort().
var longest = 0;

for (var i = 0; i < longWords.length; i++) {
    if ( longWords[i].length > longest.length ) {
        longest = longWords[i];
    }
}

I could be easy to print the longest word if you sort the array and print the first element arr.[0]


var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"];
var sorted = longWords.sort((a, b) => b.length - a.length );

console.log(sorted[0]);
// "Czechoslovakia"

There's another nice way to get the longest element, and that is with the reduce method:

var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"];

var longest = longWords.reduce(
  (a, b) => a.length >= b.length ? a : b
);

console.log(longest);

let longest = 0;
for (let i = 0; i < longWords.length; i++) {
  let word = longWords[i];
  if (longest < word.length) {
    longest = word.length;
  }
}
console.log(longest);
发布评论

评论列表(0)

  1. 暂无评论