# Sum All Numbers in a Range. Can't create an array of all numbers in the range using a for loop

Sum All Numbers in a Range. Can't create an array of all numbers in the range using a for loop
0

#1

Tell us what’s happening:

So my game plan here was to sort the two numbers in ascending order, then make an array of all the numbers between the two given numbers via a for loop, then add all those together using arr.reduce. I read the solution in the meantime and realize there’s a better way to do it, but I can’t figure out why my solution isn’t working. My attempted solution gave me an infinite loop warning, and I can’t seem to figure out why. I tried both a for and while loop and got the same error. There’s probably something super obvious I’m missing, but I can’t seem to hammer it down. Can someone set me straight? Here was my attempt:

Side note, I was having the function return inbetween in a debugging attempt.

``````
function sumAll(arr) {
arr.sort(function(a, b){return a-b});
const inbetween = []

for (i=0; arr[0] < i < arr[1]; i++){
inbetween.push(i);
}

return inbetween;
}

sumAll([1, 4]);

User Agent is: <code>Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0</code>.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sum-all-numbers-in-a-range``````

#3

Try adding “let” before you use the i in the for loop…

``````arr[0] < i && i < arr[1];