There's also a nasty (but shorter) eval hack to use in place of reduce. You can use .join(+) to convert the array into a string containing each number joined by the '+' sign, then evaluate that string as if it's a JavaScript expression to get the sum:
Ruby
JavaScript
It's a shame getting a range and a sum isn't quite as easy in JS.
Or generate the range with the es6 spread operator:
There's also a nasty (but shorter)
eval
hack to use in place of reduce. You can use.join(+)
to convert the array into a string containing each number joined by the '+' sign, then evaluate that string as if it's a JavaScript expression to get the sum:It's a bad practice to use eval, of course, but useful for JS code golf.
Or with lodash: