Home how to use negative values in for loops

# how to use negative values in for loops

iamlearning
1#
iamlearning Published in 2018-02-13 02:19:12Z
 i am trying to get the sum of odd numbers with values starting from -300,000 to 300,000 and i am getting my result as ZERO can anybody help why? var sum=0; for(var i= -300000;i<=300000;i++){ if(i%2!==0){ sum=sum+i } } console.log(sum);  what is wrong with my code
Gilles Quenot
2#
Gilles Quenot Reply to 2018-02-13 02:26:26Z
 Try this simplified code (with a bit a debug) to understand that there's just a math issue : for (var i= -30;i<=30;i++){ console.log('i == ' + i); if (i%2!==0) { sum+=i; console.log('sum is now ' + sum); } } 
 You need to login account before you can post.
Processed in 0.376782 second(s) , Gzip On .