2

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.

1
  • 2
    Do you need to handle the use case where two words are of equal length, and are both the longest? Commented Nov 14, 2013 at 20:12

6 Answers 6

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

Should probably be

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

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

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:

1
var longest = 0;

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

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

0
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);
1
  • 2
    Hey Sekh Soyeb Ali, welcome to Stack Overflow :) Your question was flagged as low quality on here because you haven't explained your answer. I can see it's some simple code but try and write a little explanation when you answer!
    – Zach Jensz
    Commented Apr 25, 2022 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.