Skip to content

Instantly share code, notes, and snippets.

@mdixon47
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save mdixon47/b408c38894dd71e623f9 to your computer and use it in GitHub Desktop.

Select an option

Save mdixon47/b408c38894dd71e623f9 to your computer and use it in GitHub Desktop.
FCC Sum All Numbers in a Range Challenge (REVISIT SOLUTION)
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