Skip to content

Instantly share code, notes, and snippets.

@ashlynnpai
Created September 18, 2017 16:48
Show Gist options
  • Save ashlynnpai/5218b9e03bafd8149fa7474f6ed9ac5b to your computer and use it in GitHub Desktop.
Save ashlynnpai/5218b9e03bafd8149fa7474f6ed9ac5b to your computer and use it in GitHub Desktop.
//sum array of two numbers and all numbers in between
//positive numbers only
function sumAll(arr) {
var min = arr.reduce(function(a, b) {
return Math.min(a, b);
});
var max = arr.reduce(function(a, b) {
return Math.max(a, b);
});
if (min == max) {
return min*2;
}
if (min < 0) {
return "abort";
}
return sumNums(min);
function sumNums(x) {
if(x == max) {
return max;
}
return x + sumNums(x + 1);
}
}
sumAll([5, 10]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment