简体   繁体   中英

Find the longest word/string in an array

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.

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

应该是

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 comes 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:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM