-1

Here is an example array:

var friendName = ['RupaKhan','Sunny','Rohul','Mistry','Zumman','Asif','Liza','Fulo'];

How do you find the longest name in the array? But you have to use a function with return as well as find as largest name.

I tried this method but it doesn't show a result:

enter image description here

Brian McCutchon
  • 8,354
  • 3
  • 33
  • 45
  • [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) Anyway, you have typos in your code. – Brian McCutchon Feb 15 '21 at 04:14
  • You are returning the length instead of name. Try saving name and check `if (largeName.length > friendlist[i].length) largeName=friendlist[i]` – Rajesh Feb 15 '21 at 04:20

1 Answers1

0

var friendName = ['RupaKhan','Sunny','Rohul','Mistry','Zumman','Asif','Liza','Fulo'];

function mega(arr){
   return arr.reduce((max,name)=>{
        return name.length > max.length? name: max
    },arr[0])
}

console.log(mega(friendName))
naresh_321
  • 119
  • 8