theonlychase
9/28/2018 - 9:33 PM

Sum All Numbers in a range

We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first.

function sumAll(arr) {
  var max = Math.max(...arr);
  var min = Math.min(...arr) + 1;
  var newArr = [];
  for (var i = min; i < max; i++) {
   newArr.push(i); 
  }
  let finalArr = [...newArr, ...arr];
  return finalArr.reduce((acc, val) => {
    return acc + val;
  });
}

sumAll([1, 4]);