Last active
August 29, 2015 14:26
-
-
Save mdixon47/b408c38894dd71e623f9 to your computer and use it in GitHub Desktop.
FCC Sum All Numbers in a Range Challenge (REVISIT SOLUTION)
This file contains hidden or 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(arr) { | |
| function range(s, e) { | |
| if (e < s) return []; | |
| else return [e].concat(range(s, e - 1)); | |
| } | |
| arr = arr[0] > arr[1] ? [arr[1], arr[0]] : arr; | |
| return range(arr[0], arr[1]).reduce(function (a, b) { return a + b; }); | |
| } | |
| sumAll([1, 4]); | |
| // Look into | |
| // http://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-an-array-based-on-suppl | |
| // Array.apply(null, Array(5)).map(function (_, i) {return i;}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment