# Sum all the numbers

**URL:** <https://forum.freecodecamp.org/t/sum-all-the-numbers/181070>\
**Category:** General\
**Created:** [March 22, 2018, 12:42pm UTC](https://forum.freecodecamp.org/t/sum-all-the-numbers/181070 "2018-03-22T12:42:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![1ncontr0L](https://avatars.discourse-cdn.com/v4/letter/1/bbe5ce/32.png) [@1ncontr0L](https://forum.freecodecamp.org/u/1ncontr0L)\
**Post date:** [March 22, 2018, 12:42pm UTC](https://forum.freecodecamp.org/t/sum-all-the-numbers/181070/1 "2018-03-22T12:42:30Z")

</div>

hey guys, can anyone clear this out:

This is the correct way:

```auto
function sumAll(arr) {
  var min = Math.min(arr[0], arr[1]);
  var max = Math.max(arr[1], arr[0]);
  var total = 0;
for(var i=min; i<=max; i++){
  total += i; } 

  return total; 

}

sumAll([1, 4]);

```

This is FALSE:

```auto
function sumAll(arr) {
  var min = Math.min(1, 4);
  var max = Math.max(1, 4);
  var total = 0;
for(var i=min; i<=max; i++){
  total += i; } 

  return total; 

}

sumAll([1, 4]);

```

How come?

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [March 22, 2018, 1:00pm UTC](https://forum.freecodecamp.org/t/sum-all-the-numbers/181070/2 "2018-03-22T13:00:47Z")

</div>

The second snippet hard-codes 1 and 4 as the minimum and maximum values. The values that you plug in to `Math.min` and `Math.max` should come from the array that you pass to the function. By hard-coding the values, the function will only ever sum the numbers from 1 to 4. The array is entirely ignored.

---

<div class="post-metadata">

**Author:** ![sculprog](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sculprog/32/158756_2.png) [@sculprog](https://forum.freecodecamp.org/u/sculprog)\
**Post date:** [March 22, 2018, 3:10pm UTC](https://forum.freecodecamp.org/t/sum-all-the-numbers/181070/3 "2018-03-22T15:10:17Z")

</div>

Just to add, if the SumAll function is passed the array [1, 4] it will **appear** to work for that array but it is working exactly as kevcomedia said. 🍻

---

<div class="post-metadata">

**Author:** ![1ncontr0L](https://avatars.discourse-cdn.com/v4/letter/1/bbe5ce/32.png) [@1ncontr0L](https://forum.freecodecamp.org/u/1ncontr0L)\
**Post date:** [March 23, 2018, 1:30pm UTC](https://forum.freecodecamp.org/t/sum-all-the-numbers/181070/4 "2018-03-23T13:30:45Z")

</div>

same goes here:

```auto
function translatePigLatin(str) {

var first = str.match(/[aeiou]/);
var firstPosition = str.indexOf(first);

if(firstPosition > 0) {
return str.slice(firstPosition) + str.slice(0, firstPosition) + "ay"; }
  return str + "way";
}

translatePigLatin("consonant");

```

as you can see "str.slice(0, firstPosition) \<-- it does recognize “0” which is equal to “C” but if i change to “str.slice(0, 1)” it will be completely lost, since he will not recognize the “1” which is “o”… This is pretty weird to me

---

<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 17, 2021, 8:24pm UTC](https://forum.freecodecamp.org/t/sum-all-the-numbers/181070/5 "2021-01-17T20:24:03Z")

</div>

Closed on January 17, 2021, 8:24pm UTC


