'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.



Solution 1:[1]

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

Should probably be

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

Solution 2:[2]

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

Solution 3:[3]

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:

Solution 4:[4]

var longest = 0;

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

Solution 5:[5]

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

Solution 6:[6]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jbabey
Solution 2
Solution 3 David Thomas
Solution 4 adeneo
Solution 5
Solution 6 Sekh Soyeb Ali