+ 1

Hi..could someone please explain to me how the EVEN values is achieved in the 'for loop'

14th Feb 2019, 4:54 PM
Irene Aruto
Irene Aruto - avatar
6 Answers
+ 2
if divide by 2 == 0 even else odd or the use of not if /2 !=0 odd else even checking one is kind of testing both like boolean. the for loop bit will just be traversing each item in the loop til all are done lol
14th Feb 2019, 5:23 PM
peter
peter - avatar
+ 1
var age =0; for(;age<=40 ; age+=2){ document.write(age); }
14th Feb 2019, 5:04 PM
Irene Aruto
Irene Aruto - avatar
+ 1
Okay! Great thanks
14th Feb 2019, 5:08 PM
Irene Aruto
Irene Aruto - avatar
+ 1
Irene Aruto that code will work, but a better way will be: for (var n = 0; n <= 40; n++) { if (n % 2 == 0) { document.write(n) } } see you code is only counting in twos, but if e.g the user entered a number and if its even you want to do A if odd do B the above if statement will check if that input number is even or odd
14th Feb 2019, 5:15 PM
Mind To Machine đŸ’»đŸ•†
Mind To Machine đŸ’»đŸ•† - avatar
0
please make a go at it first, write a code that showed you try, then we will help you😉
14th Feb 2019, 4:56 PM
Mind To Machine đŸ’»đŸ•†
Mind To Machine đŸ’»đŸ•† - avatar
0
if something divides by 2 evenly no remainder its even if it does not divide by 2 evenly and had say a remainder 1 its odd
14th Feb 2019, 5:06 PM
peter
peter - avatar