Created
September 18, 2017 16:48
-
-
Save ashlynnpai/5218b9e03bafd8149fa7474f6ed9ac5b to your computer and use it in GitHub Desktop.
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
//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