Skip to content

Instantly share code, notes, and snippets.

@jwill9999
Created October 10, 2017 16:14
Show Gist options
  • Save jwill9999/91a696aad213fd978ccb5ae62329ed50 to your computer and use it in GitHub Desktop.
Save jwill9999/91a696aad213fd978ccb5ae62329ed50 to your computer and use it in GitHub Desktop.
Sum All Numbers In a Range
/*
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