Summing numbers in a range

Summing numbers in a range
0.0 0

#1

Hi Everyone. I am trying to find the sum of numbers in a range. My code works for all examples like 3, 6 --> 18 and 5, 10 ---> 45 , however when I try to do 1,1, it should return 1 but it isnt. Anyone mind lending a hand? See code below


function sumRange(from, to) {
  var result = from;
  if(to > from) {
    while (to > from) {
      from ++;
      result += from;
    }
    return result;
  } else if (from > to) {
    result = to;
    while (from > to ) {
      to++;
      result += to;
    }
    return result;
  }

}

Thanks in advance!


#2

When from and to are equal to each other, both of your if conditions are false.


#3

The reason sumRange(1, 1) returns undefined, is because your first if statement checks if 1 > 1,
Since 1 is not greater than 1, it evaluates to false and then else if statement asks if 1 > 1 ylelding the same evaluation of false, so nothing happens again.

Even though there is no return statement at the end of your function, JavaScript returns undefined for you.


#4

ahhhhh >=!! Thank you so much


#5

thanks so much!!! appreciate the help