Intermediate Algorithm Scripting: Sum All Numbers in a Range_Code not working

Hello. Could someone tell me why my code is not working? I don’t understand why it’s not passing these two tests:

sumAll([4, 1]) should return 10
sumAll([10, 5]) should return 45.

function sumAll(arr) {
  let sorted = arr.sort(function(a,b){
  let newArr = [];
  for (let i = sorted[0]; i<=sorted[1]; i++){
  let sum = 0;
  for (let j = 0; j < newArr.length; j++) {
    sum += newArr[j]; 
  return sum;

sumAll([1, 4]);

If you do not return a value in a function, then the default return value is undefined. So, your sort callback function is returning undefined over each iteration of the the arr.

On a side note, since the sort method sorts the array in place and mutates it, there really is no need to create an additional reference to arr by assigning it to sorted. You could just reference arr in the rest of your code instead of sorted, because they both reference the same array.

1 Like

I was curious what the one line of code answer was for this problem