Created
August 31, 2016 11:29
-
-
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
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
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; | |
} |
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
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