Here is my solution to this problem. Can this be less verbose?
function sumAll(arr) {
//Sort array so smallest number starts from index 0;
arr = arr.sort((a,b) => a - b);
//iterate and sum all numbers;
let sum = 0;
for(let i = arr[0]; i <= arr[1]; i++){
sum += i;
};
//return sum of all numbers
return sum;
};
Making an array only to sum the contents of the array is generally not a good idea. Making an array one element at a time with a loop like that is pretty slow, relatively speaking, and it’s it’s just not a best practice.