Sum Nmbers in Range (using maths formula...neat cheat)

Hi, thought I’d use some old well known maths algorithm for the sum of an arithmetic series, after sorting the array, taking the difference plus 1 to get the number of terms etc…

function sumAll(arr) {

let sortArr = arr.sort( (a, b) => a > b);

let n = (sortArr[1] - sortArr[0] + 1);

return n/2 *[2 * sortArr[0] + (n - 1)*1 ];

}

sumAll([1, 4]);

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

We have set your post to unlisted. Thanks for your understanding.