Skip to content

Instantly share code, notes, and snippets.

@arsho
Created August 31, 2016 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arsho/d0ef4338362be2c7bc540323fac01c15 to your computer and use it in GitHub Desktop.
Save arsho/d0ef4338362be2c7bc540323fac01c15 to your computer and use it in GitHub Desktop.
Sum All Numbers in a Range, Freecodecamp challenge, URL: https://www.freecodecamp.com/challenges/sum-all-numbers-in-a-range
function sumAll(arr) {
var min = Math.min(arr[0] , arr[1]);
var max = Math.max(arr[0] , arr[1]);
var sum = 0;
for(var i=min ; i<=max ; i++) {
sum+=i;
}
return sum;
}
function sumAll(arr) {
var min = Math.min( arr[0] , arr[1]);
var max = Math.max( arr[0] , arr[1]);
return ((max*(max+1))/2-(min*(min-1))/2);
}
sumAll([1, 4]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment