Array Not Reducing With Reduce Method

Can someone explain to me why my array is not getting reduced? Instead I am getting an “undefined” output.

Hi,

Where are you storing the return value?

I tried it and it misses just one keyword to work.

Cheers.

1 Like

The sum function is undefined because it has no return value.

Something to be mindful of is that when you right a function definition that has curly brackets { } , you must explicitly return a value.

However, if you refactor your sum function, since the return value is a single line, you can implicitly return the value by dropping the { }s

i.e. - let sum = array => array.reduce( (a,c) => a +c)

You actually did this in the inner reduce method, possibly not even realizing it, as many of the methods requiring call backs use this style of returning as their default example… but… none the less you can employ the same methodology in your function definition.

2 Likes

thank you @UniqueNospaceShort !

thank you @mechanicDev !!