Sum All Numbers in a Range confusion

Tell us what’s happening:
Hi everyone,
I am trying to understand what’s going on with my code. It seems that if arr[1] >= 10, the result is always 0. What am I doing wrong? Any help would be appreciated.

Your code so far


"use strict";

var sumAll = (arr) => {
  arr.sort();

  let sum = 0,
      min = arr[0],
      max = arr[1];

  for(let i=min;i<=max;i++) sum += i;

  return sum;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36.

Link to the challenge:

The clue you need is in the instructions “The lowest number will not always come first.”

What about arr.sort() ? Doesn’t it change the original state of the array?

the sort() function by default sorts by converting data to the unicode and then sorting, unless you give it a function to execute. Here is the link to the MDN article:


And try this version of the code.

arr.sort(function(a, b) { return a - b; });

@zakattack194 Thank you very much for the clarification.

1 Like