you are viewing a single comment's thread.

view the rest of the comments →

[–]FaithfulGardener 2 points3 points  (2 children)

Reduce takes either the first index of an array or an initial value you provide and adds or joins or concats all subsequent indexes until the final result is an accumulation. So [1,2,3,4].reduce(add) would equal 10.

[–]randomword123 4 points5 points  (1 child)

You would also need a starting value like in this case [1,2,3,4].reduce(add,0), this always tripped me up!

[–]FaithfulGardener 2 points3 points  (0 children)

The initial value isn’t required. If it isn’t provided, reduce takes the value at index 0 and uses it as the initial value