0

Why the answer is 2 instead of 1 here?

Fill in the blanks to print EVEN values from 0 to 20 using for a loop: var x= 0 for ( ; x <= __; x +=_) { document.write(x); }

14th Mar 2020, 5:38 AM
Umidbek
Umidbek - avatar
3 Réponses
+ 1
Because it is more efficient to increase <x> value by two. The next even number is always 2 number away from the first anyways. If <x> value was incremented by 1, then there will be more iterations needed, and there will be a need to check whether <x> was even or odd. The same method can also be used to print odd values. But initial value of <x> must be an odd value too. (Edit) Please tag a relevant language -> Javascript
14th Mar 2020, 5:48 AM
Ipang
+ 1
Thank you
14th Mar 2020, 6:42 AM
Umidbek
Umidbek - avatar
+ 1
No problem 👌
14th Mar 2020, 6:45 AM
Ipang