Created
October 10, 2017 16:14
-
-
Save jwill9999/91a696aad213fd978ccb5ae62329ed50 to your computer and use it in GitHub Desktop.
Sum All Numbers In a Range
This file contains hidden or 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
/* | |
We'll pass you an array of two numbers. Return the sum of those two numbers and all numbers between them. | |
The lowest number will not always come first. | |
*/ | |
function sumAll(arr) { | |
var newArray = []; | |
var result; | |
(function createArray(arr) { | |
for (var i = Math.min(...arr); i <= Math.max(...arr); i++) { | |
newArray.push(i); | |
} | |
})(arr), add(newArray); | |
function add(newArray) { | |
result = newArray.reduce(function(sum, value) { | |
return sum + value; | |
}, 0); | |
} | |
return result; | |
} | |
//sumAll([1, 4]); //should return 10. | |
//sumAll([4, 1]); //should return 10. | |
sumAll([5, 10]); //should return 45. | |
//sumAll([10, 5]); //should return 45. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment