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?
Your code so far
function sumAll(arr) {
arr.sort((a,b)=> a-b);
let newArr=[];
if (arr[0] != arr[1]){
for (let i = arr[0];i<=arr[1];i++)
{newArr.push(i)};
return newArr.reduce((a, b) => a + b);
}
return arr[0]+arr[1]
}
sumAll([1, 5]);
console.log(sumAll([4,4]))
Your browser information:
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.
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();
// copy arr with spread
const copy = [...arr];
// using destructuring to get min, max from sorted arr
const [min, max] = copy.sort((a, b) => a - b);
// using spread with Math.min and Math.max
const min = Math.min(...arr);
const max = Math.max(...arr);