Ask coding questions

← Back to all posts
how to Get the longest string in an array
hamidazizy (0)

function getLongestString(strings) {


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

malvoliothegood (191)

In your function you need to:
1) Create a variable to store the longest string in - could be called longest
2) Initialise it to the first item in the array
3) Iterate (loop) through the array and compare the current array item's length to that of longest
4) If the length of the current item is greater than the length of longest set its value to be the current item
5) After the loop finishes return longest