Cofounded Host Collective (DiscountASP.net). Cofounded Player Axis (Social Gaming). Computer Scientist and Technology Evangelist with 20+ years of experience with JavaScript!
Sum is just about the only case where reduce is useful, but in reality it should never be passed around like that. Why keep an add function around that is only meant to be used together with a reduce? Just wrap it once and never use reduce again:
I think you mean
You need a seed value.
Nope. That is exactly what I meant.
The seed is optional. If you don't provide a seed, the first item in the array is given as the accumulator.
Run the code!
Sum is just about the only case where reduce is useful, but in reality it should never be passed around like that. Why keep an
add
function around that is only meant to be used together with areduce
? Just wrap it once and never use reduce again:Don't tell me
arr.reduce(add)
makes more sense thansum(arr)
because it doesn't.In reality you can write
sum
event more efficiently with a regular loop and every part of your code benefits.