Sum All Numbers in a Range. Why without Max and min won't work?

Tell us what’s happening:

Your code so far

function sumAll(arr) {
  arr = arr.sort(function(a,b){
    return a-b;
  });
  var result = 0;
  for(var i = arr[0]; i < arr[arr.lenght-1]; i++){
    result += i;
  }
  return result;
}

sumAll([1, 4]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/sum-all-numbers-in-a-range

Your code has two issues preventing you from solving the challenge.

#1) You have a typo in the following line.

#2) Even if you fix the typo, you are only going to be summing all the numbers starting with the lowest up to but not including the highest number. You just need to make a slight modification to your for loop condition and it will work.

1 Like

thanks. I found the typo “length” and change it to i <= arr[arr.length-1]; And it Worked!