Returns the longest string in an array (JavaScript)

So, I'm trying to find the longest string in an array of strings. I made a problem like this before I had to return the length of the longest string. The thing is, my code works and returns 11 when it looks like this:

var long1= 0; var plorp = ["bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]; function longestString(arr){ for (i=0; i<arr.length; i++){ if (arr[i].length > long1){ long1= arr[i].length; } } return long1; } 

but if I change long1= arr[i].length; on long1 = arr[i]; , it just returns arr[0] . Am I missing something? The loop seems to be iterating correctly.

Edit: this means that it returns bbllkw .

+5
source share
7 answers

You just have to change the long declaration to:

 var long1= ''; 

and in a for loop condition it should be

 arr[i].length > long1.length 
+2
source

Instead, you can use reduce :

 var plorp = ["bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]; var longest = plorp.reduce(function(a, b) { return a.length > b.length ? a : b }, ''); console.log(longest); 

Or ES6 version:

 var plorp = ["bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]; var longest = plorp.reduce((a, b) => a.length > b.length ? a : b, ''); console.log(longest); 
+6
source

Updated after the user was offered a more basic solution.

Here we find the longest length of an array element using the reduce function, and then filter the array with elements that have this length using the filter function. It returns us several elements if they have the same but the longest length.

 var plorp = ['sameLength', 'someoth', 'asfzc', 'sameLLngth']; ln = plorp.reduce((r,s) => r > s.length ? r : s.length, 0); const result = plorp.filter(pl => pl.length == ln); console.log(result); 

Old answer

If there is more than one longest string in the array, it will return an array of them. If there is only one long, it will return a string, not an array.

 var plorp = ["bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]; var wholeArr = []; function longestString(arr) { var tlength = 0; for(var i =0; i < plorp.length; i++){ if(tlength < plorp[i].length){ tlength = plorp[i].length; } } for(var j =0; j < plorp.length; j++){ if(plorp[j].length == tlength){ wholeArr.push(plorp[j]); } } if(wholeArr.length == 1){ return wholeArr[0] }else{ return wholeArr } } console.log(longestString(plorp)); 
+1
source

Just compare the length of the lines, and then assign the line itself to long1.

 var plorp = ["bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]; function longestString(arr) { var long1 = arr[0]; for (i = 0; i < arr.length; i++) { if (arr[i].length > long1.length) { long1 = arr[i]; } } return long1; } console.log(longestString(plorp)); 
0
source

The problem is that you are installing

 long1=arr[i]; 

And you compare with the length, which is an integer value.

First, the iteration compares the integer value 0 with the length of the first line, which, of course, makes the result of the comparison expression true, and you set

  long1=arr[0]; 

For the next iteration, long1 no longer contains an integer value, but a string, so the comparison between length and long1 always returns false.

That is why you always get the first line as a result.

You need to initialize long1 as an empty string and use long1.length to compare as suggested by belhadj's answer.

0
source

Another solution includes a sort array and a comparison function, but it may be a "heavy" MDN solution:

Depending on the compareFunction property, this can lead to high overhead. The more the compareFunction function works, the more elements you need to sort, the wiser it might be to consider using a map for sorting.

So, taking the previous tip, here is one way to get the longest string using a map in combination with the array sort method as follows:

 var plorp = ["bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]; function getLongestStr() { var mapped = plorp.map((el, i) => ({ index: i, length: el }) ); mapped.sort((a, b) => { if (a.length > b.length) { return -1; } if (a.length < b.length) { return 1; } return 0; }); // DESC order so longest string in 0th element return mapped.map((el) => plorp[el.index])[0]; } console.log( getLongestStr() ); 

Interesting discussion here .

0
source

var arr = ["first", "second", "third", "four", "thousand"]

function getLongestString (arr) {let longestStringArr = arr.sort ((a, b) => a.length - b.length) .reverse (); return longestStringArr [0]; }

console.log (getLongestString (arr))

0
source

Source: https://habr.com/ru/post/1271652/


All Articles