Last active
January 31, 2016 21:37
-
-
Save sionleroux/03fe6fc0acb0f16431a6 to your computer and use it in GitHub Desktop.
Functional solution to Free Code Camp's "Sum All Numbers in a Range" challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumAll(x) { | |
return Array.from( | |
{length: Math.max(...x) - Math.min(...x) + 1}, | |
(v,k) => k + Math.min(...x) | |
).reduce((s, c) => s + c) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
x
is an arrayelements will be
(max - min + 1)
, the+1
is needed to include the upper value, e.g.5..10 = (10 - 5 + 1) = (5 + 1) = 6
k
already increment)c
into sums
min()
andmax()
expect several numbers as arguments, not a single array,...x
is how we spreadx
into arguments