# Sum all numbers in a range (question)

let says the numbers are (4,4), it has to sum 4+4? or it should give the 4 as a result since there is no numbers in between?

``````
function sumAll(arr) {
arr.sort((a,b)=> a-b);
let newArr=[];
if (arr != arr){
for (let i = arr;i<=arr;i++)
{newArr.push(i)};
return newArr.reduce((a, b) => a + b);
}
return arr+arr
}

sumAll([1, 5]);
console.log(sumAll([4,4]))
``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36`.

Challenge: Sum All Numbers in a Range

For my understanding that would be 4, because the range from 4 to 4 is only 4.

2 Likes

Be careful with `sort`, it mutates the array in place, so you’ve caused a side effect by using it directly on the `arr` parameter.

You should copy the array via `slice` or the spread syntax and then call `sort` on the copy. Or use a different method like `Math.min` and `Math.max` to get the bounds.

``````// copy arr with slice
const copy = arr.slice();