# My solution to sum in a range

I saw there were no negative numbers so I threw in a tweaked formula, what do you think?

``````function sumAll(arr) {
var greater = Math.max(arr[0], arr[1] );
var smaller = Math.min(arr[0], arr[1]);
return (greater*(greater+1)/2) - (smaller*(smaller-1)/2);
/**
(arr[1]*(arr[1]+1)/2) - (arr[0]*(arr[0]-1)/2);
*/
}

sumAll([1, 4]);
``````

Why do you think it won’t work with negative numbers? I tried it with negative numbers and it seemed to work fine.

My solution is also maths-based. I can’t remember how I got this formula though.

``````function sumAll(arr) {
var min = Math.min(...arr);
var max = Math.max(...arr);
return (max + min) * (max - min + 1) / 2;
}
``````

I remembered this formula yesterday https://en.m.wikipedia.org/wiki/1_%2B_2_%2B_3_%2B_4_%2B_⋯so I thought on how to make it work and I did what I show you, but it seems to be only for natural numbers.

I didn’t know we could use the spread operator in the editor, I tried using let and it blamed me.