Function getLongestString

Would someone be so kind as to walk me through this problem?  Thanks!

function getLongestString(strings) {


}

/* Do not modify code below this line */

const strings = ['long', 'longer', 'longest'];
console.log(getLongestString(strings), '<-- should be "longest"');

Hi @CoBe, so far what have you tried ?

honestly a lot of other things.... I just deleted them and kept starting over.
I'm a brand new coder and I'm trying.....

let getLongestString = ” longest”;

getLongestString.length;

The function getLongestString will be called and tested on different arrays that contains different words (not only the ones in the code example).
The purpose of the function is in fact that to be reused with different parameters (the values you put in the brackets when you call it).
So what you have to do is to define a set of operations that, for any given array of String that is passed as a parameter (called strings), "return"s the longest one contained in the array.
This definition has to be put in the body of the function (the part inside the curly brackets).

1 Like
function getLongestString(strings) {
let strings1 = "long";
let strings2 = "longer";
let strings3 = "longest";
return strings3.length;
}

const strings = ['long', 'longer', 'longest'];
console.log(getLongestString(strings), '<-- should be "longest"');

And if you don’t want to use a for loop and a flag or early return:

I think before you try to code something you have to understand what your input variable(s) are and what your expected output is.

The code you wrote here will define a function getlongeststring that takes a strings variable (in this case the function is run with the array strings)

Your function then defines 3 separate variables inside and then returns the length of the strings3 variable and therefore return 7 which is the length of the string strings3.

This isn’t what the expected output of the function should be because it should be returning the string “longest” which is the longest string in the array called strings.

Your objective here is to find a way to loop through the strings array containing the 3 strings and perform some kind of logic to return the longest string.

There are multiple ways of solving this problem so I’ll leave it up to you to implement it.

1 Like

This in not ok because it will work only on one specific instance of the strings array. It has to work for any array passed to it. Like for [“cat”, “penguin”, “fish”, “lion”, “elephant”]. The fact that the example says it should return “largest” means that in that example it should act that way.

You need to work on the strings included in the array, not define your own ones

You cannot get the value with assigning variables. Try a loop.