Sum of all numbers in the range

Sum of all numbers in the range
0

Tell us what’s happening:
why is it logging 0 for everything i try ,even if i use push into an empty array its returning

Your code so far


function sumAll(arr) {
 let temp=0;
let max = Math.max(arr[0],arr[1])
 let min= Math.min(arr[0],arr[1]);
for(var i=max;i<=min;i--){
   temp += i
}
return temp
}
console.log(sumAll([1, 4]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Sum All Numbers in a Range

Link to the challenge:

for(var i=max;i<=min;i--){

If you use that line:
|0|1|
|1|4|
is the outcome resulting eventually in an outcome of 0 in your code is that what you want?

that line is same as
for(i=4;i<=1;i–)

i dint get you what you are trying to say

Maybe trying to visualize it would help you
http://pythontutor.com/visualize.html#mode=display

1 Like

Your relational operator seems wrong.

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

It should be:

for(var i=max;i>=min;i--){
   temp += i
}

because i is max therefore you test if i >= min still not i <= min.

1 Like

thanks ! that helped