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.
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: