why this is not printing 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
31st Aug 2020, 5:34 PM
Mr. 12
Mr. 12 - avatar
6 Answers
+ 2
Because you said i>0 me ans until i is greater than zero. you can use i>=0 then it will be correct. Let me explain in deep: result = []; for (i = 4; i > 0; i -= 2){ result.push(i); } I = 4, I is greater than zero so i-2 I = 2, I is greater than zero so I-2 I = 0, but 0 is not bigger than 0 so loop will end
31st Aug 2020, 5:49 PM
Bhavik Mahalle
Bhavik Mahalle - avatar
+ 1
Because you write: i > 0 But this will not print 0 because (i) is greater than 0 For solving this problem just add: i >= 0 Now (i) is equal or greater than 0 and it will print 0 too.
31st Aug 2020, 5:49 PM
Alireza
+ 1
I working like this I=4>0; Print 4 //in result now I= 2 I=2>0 Print 2 //in result Now I=0 I=0 not >0 Hence no print.//in result hope I clear my view to u.
31st Aug 2020, 6:03 PM
Divya Mohan
Divya Mohan - avatar
0
Because condition is I>0 Use I >= 0 to get 0 also
31st Aug 2020, 5:46 PM
Divya Mohan
Divya Mohan - avatar
0
Divya Mohan but, when i = 2, 2 > 0, so i = 2-2 so i becomes 0 then i = 0, 0>0 (false) so it wont go for 3rd condition. but i value still remains 0 right ?
31st Aug 2020, 5:49 PM
Mr. 12
Mr. 12 - avatar
0
so it will push the value to the array only if second condition is true?
31st Aug 2020, 5:57 PM
Mr. 12
Mr. 12 - avatar