Sum All Numbers in a Range sort method

Sum All Numbers in a Range sort method
0.0 0

#1

Tell us what’s happening:
Well, I don’t understand how working method sort()

Your code so far

function sumAll(arr) {
  arr = arr.sort()
  let sum = arr[0] + arr[1]
  for(let i = arr[0] + 1; i < arr[1]; i++ ) {
    sum += i
  }
  return sum
}
sumAll(5, 10) // Why it's return unsorted array?  because of this loop not working

method sort() sorting array and return a sorted array, but not correct for me. If I pass in method 5 and 10 - method not sorting array, but if i pass 5 and 9 - it’s sorted - why? I already solve this problem, but it took me
сhange the sorting mechanism:

arr.sort((a, b) => a < b ? 0 : 1)

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range


#2

The default sort function sorts by unicodes. It does pretty well for letters but not for numbers. That’s why you have to pass in a callback function to correctly sort numbers.