Question

Modify the function to return the longest string in the given array. function getLongestString(strings) { }...

Modify the function to return the longest string in the given array.

function getLongestString(strings) {

}

/* Do not modify code below this line */

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

function getLongestString(strings)

{

var i;

var len=strings[0].length;

var index=0

for (i = 1; i < strings.length; i++)

{

if(len<strings[i].length)

{

index=i;

len=strings[i].length;

}

}

return strings[index]

}

/* Do not modify code below this line */

const strings = ['long', 'longer', 'longest'];

console.log(getLongestString(strings), '<-- should be "longest"');

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Modify the function to return the longest string in the given array. function getLongestString(strings) { }...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT