# Sum Numbers in a range

**URL:** <https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392>\
**Category:** General\
**Created:** [May 13, 2017, 8:24pm UTC](https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392 "2017-05-13T20:24:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TeyimPila](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/teyimpila/32/49707_2.png) [@TeyimPila](https://forum.freecodecamp.org/u/TeyimPila)\
**Post date:** [May 13, 2017, 8:24pm UTC](https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392/1 "2017-05-13T20:24:17Z")

</div>

Hello, I just solved the sum all numbers in a range challenge but I got a strange behavior in the process

I first did this…

```
var total = 0;
function sumAll(arr) {
  var min = Math.min.apply(null, arr);
  var max = Math.max.apply(null, arr);

  for(var i = min; i <= max; i++){
    total += i;
  }

  return total;
}
sumAll([5, 10]);

```

only the first test that checks if the output is a number passed, though the outputs were correct. However, when I did the following, which is basically changing the scope of the total variable, it was accepted

```
function sumAll(arr) {
  var min = Math.min.apply(null, arr);
  var max = Math.max.apply(null, arr);
  var total = 0;

  for(var i = min; i <= max; i++){
    total += i;
  }

  return total;
}
sumAll([5, 10]);

```

WHY???

---

<div class="post-metadata">

**Author:** ![xShohagh](https://avatars.discourse-cdn.com/v4/letter/x/ccd318/32.png) [@xShohagh](https://forum.freecodecamp.org/u/xShohagh)\
**Post date:** [August 23, 2017, 2:04am UTC](https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392/3 "2017-08-23T02:04:06Z")

</div>

**This is what I did. It works.**

```auto
function sumAll(arr) {
  var sum = 0;
  if(arr[1]>arr[0]){
  for(var i = arr[0]; i <= arr[1]; i++){
    sum = sum + i;
    }
  }
  else{
    for(var j = arr[0]; j >= arr[1]; j--){
    sum = sum + j;
    }
  }
  return sum;
}

sumAll([4, 1]);

```

---

<div class="post-metadata">

**Author:** ![ThomasShipe](https://avatars.discourse-cdn.com/v4/letter/t/e56c9b/32.png) [@ThomasShipe](https://forum.freecodecamp.org/u/ThomasShipe)\
**Post date:** [August 23, 2017, 4:07am UTC](https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392/5 "2017-08-23T04:07:01Z")

</div>

congrats.I will try with this later.

---

<div class="post-metadata">

**Author:** ![AleaxanderMagno](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/aleaxandermagno/32/92130_2.png) [@AleaxanderMagno](https://forum.freecodecamp.org/u/AleaxanderMagno)\
**Post date:** [July 10, 2018, 3:06am UTC](https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392/6 "2018-07-10T03:06:14Z")

</div>

### I am wandering if there is a way to create the numbers without using for.....

I mean for example using **map**. Because as I understood map method return an array with the same input length.....

```auto
function sumAll(arr) {
let data = ([].concat(arr)).sort((a,b)=>a-b);
let newData = []

for (let i = data[0];i<=data[1];i++){
 newData.push(i)
}
return(newData.reduce((a,b)=>a +b));

}

sumAll([1, 4]);

```

---

<div class="post-metadata">

**Author:** ![fredrikif](https://avatars.discourse-cdn.com/v4/letter/f/3ec8ea/32.png) [@fredrikif](https://forum.freecodecamp.org/u/fredrikif)\
**Post date:** [September 24, 2018, 1:14pm UTC](https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392/7 "2018-09-24T13:14:08Z")

</div>

There is also a mathematical formula for adding all numbers from X to Y, which is very useful here, and lets you avoid a for loop completely.

```auto
(highestNumber - lowestNumber + 1) * (highestNumber + lowestNumber) / 2
```

---

<div class="post-metadata">

**Author:** ![Gerwyn](https://avatars.discourse-cdn.com/v4/letter/g/e79b87/32.png) [@Gerwyn](https://forum.freecodecamp.org/u/Gerwyn)\
**Post date:** [May 27, 2019, 4:10pm UTC](https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392/8 "2019-05-27T16:10:44Z")

</div>

aww wow… that’s genius i understood ur solution perfectly. good job!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 5:51pm UTC](https://forum.freecodecamp.org/t/sum-numbers-in-a-range/115392/9 "2021-01-16T17:51:27Z")

</div>

Closed on January 16, 2021, 5:51pm UTC


