Simple solution for Sum All Numbers in a Range challenge

Here’s my solution for this challenge:


function sumAll(arr) {
    
var myArray = arr;

 maxValue = Math.max.apply(null, myArray);

 minValue = Math.min.apply(null, myArray);

 var newArray = Array();

newArray.push(minValue, maxValue);

var ourTotal = 0;

for (var i = minValue; i <= maxValue; i++){
    
    ourTotal += newArray[0]++;
    
}  
    
    return ourTotal;
}

sumAll([1, 4]);


Hi. thanks, forgot to clean up the code before i pasted it here.

This is how I do it.

function sumOf(min, max) {
    return (max-min+1) * (min+max) / 2;
}

function sumAll(arr) {
    return sumOf(Math.min.apply(null,arr), Math.max.apply(null,arr));
}