DEV Community

Discussion on: 🔥 Getting the largest number from an array

Collapse
 
domhabersack profile image
Dom Habersack

I agree you could do the same with reduce if you’d prefer that. I prefer the shorter version, but that’s why it’s good there are many ways to do the same thing. I wouldn’t call JavaScript “insane” because it does some things differently.

In your example, you probably want to use Number.NEGATIVE_INFINITY instead of 0 as the initial value in case there are only negative values in the array.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

In your example, you probably want to use Number.NEGATIVE_INFINITY instead of 0 as the initial value in case there are only negative values in the array.

You can just pass it a single argument. I tried that at first but changed it because I thought that was causing my error, but then I figured out it was actually reduce passing additional garbage to Math.max.

I wouldn’t call JavaScript “insane” because it does some things differently.

It's not that it does things differently that makes insane. It's the fact that it does things differently in insane ways. Passing random stuff nobody wanted to the combining function in reduce is one such thing, but there's many many more.