JavaScript

function longest (n,v) { var val = n[0]; var length = n[0].length; for(var i = 1; i < n.length; i++){ if(n[i].length > length){ val = n[i]; length = n[i].length; } } return val }

See more:

I am given an array of strings and n which indicates how many longest strings should be returned. What I mean is this

So, if n is n, it should return only 1 string, which is the longest on the array, if n is 2, it should return the longest string and the one which is the second longest.

**What I have tried:**

I was trying to find the longest string and then remove it and then iterate again and add it but was stuck. Is it better to use recursion here?

JavaScript

longStr(["a", "ab", "abc", "abcd", "abcde"], 2)-> 'abcde abcd'

So, if n is n, it should return only 1 string, which is the longest on the array, if n is 2, it should return the longest string and the one which is the second longest.

JavaScript

function longest (n,v) { var val = "" for(var i = 0; i < n.length; i++){ var length = n[0].length; if(n[i].length > length){val = n[i] + " " +n[i]; delete n[i]} } return val } console.log(longest( ["a", "ab", "abc", "abcd", "abcde"], 2));

I was trying to find the longest string and then remove it and then iterate again and add it but was stuck. Is it better to use recursion here?

Comments

Afzaal Ahmad Zeeshan
10-Aug-16 13:10pm

Is this a question somewhere from Code Fights? They used to show such questions, with no sense at all.

try:

JavaScript

function longest (n,v) { var val = n[0]; var length = n[0].length; for(var i = 1; i < n.length; i++){ if(n[i].length > length){ val = n[i]; length = n[i].length; } } return val }

Permalink

Share this answer

try this

refer Array.prototype.sort() - JavaScript | MDN[^]

demo:- JSFiddle[^]

Updated solution based on comments from**Richard Deeming**

JavaScript

function longStr(array, max) { var result = ''; array.sort(function (a, b) { return b.length - a.length; }); // sort by length (descending) max = max >= array.length ? array.length : max; // validate for the max count less than array length for (var i = 0; i < max; i++) { result += array[i] + ' '; } return result; } longStr(["a", "ab", "abc", "abcd", "abcde"], 2);

refer Array.prototype.sort() - JavaScript | MDN[^]

demo:- JSFiddle[^]

Updated solution based on comments from

JavaScript

function longStr(array, max) { var result = ''; array.sort(function (a, b) { return b.length - a.length; }); // sort by length (descending) max = max >= array.length ? array.length : max; // validate for the max count less than array length returnarray.slice(0, max).join(' ');}

Permalink

Share this answer

v4

Comments

Richard Deeming
10-Aug-16 13:26pm

Karthik_Mahalingam
10-Aug-16 13:42pm

yes Correct, but the Op needs the answer in this format

any ways he can customise it.

updated the solution.

`'abcde abcd'`

any ways he can customise it.

updated the solution.

Maciej Los
10-Aug-16 16:57pm

Good catch!

Maciej Los
10-Aug-16 16:57pm

5ed!

Karthik_Mahalingam
10-Aug-16 21:54pm

Thank you Maciej

AlexLearne
11-Aug-16 6:23am

Thank you for your answer, also comments in the code help a lot. Very nice explanation!

Karthik_Mahalingam
11-Aug-16 6:24am

welcome:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900