Bonfire: Sum All Numbers in a Range

The Challenge:
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.

Array.reduce()
Math.min()
Math.max()

My Solution:

function sumAll(arr) {
var highestNum = Math.max.apply(null, arr);
var lowestNum = Math.min.apply(Math, arr);
var numArr = [];
for (var i = lowestNum; i <= highestNum; i++) {
numArr.push(i);
}
console.log(numArr);
var sumNum = numArr.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
});
return sumNum;
}

sumAll([1, 4]);

To approach this challenge, I broke it down into small pieces.

1. First I knew I had to get the highest number, so I used the helpful hint of Math.max() to find a way to grab the highest number from the given array.

Because the given numbers are inside of an array, just using Math.max(arr) wouldn’t work. I had to use Math.max.apply(null, arr) to get the highest number.

2. Now that I had the highest number, I needed to find the lowest number in an array.  I used Math.min.apply(Math, arr);

3. I then created a for loop to get all the numbers between my lowest number and my highest number, inclusive.