Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Someone on sololearn suggested this: "Want to print the first 5 even numbers?" in JS, and the answer was like this:

var x;
for(x=0;x<=10;x+=2){
document.write(x+" ");
}

What I have tried:

Output was 2 4 6 8 10 for the guy who posted the question, but when I tried it, it was 0 2 4 6 8 10??
Posted
Updated 4-Nov-17 0:56am

1 solution

Technically, neither of you are right: zero is an even number - Parity of zero - Wikipedia[^] - so the "First five even numbers" are 0, 2, 4, 6, 8.

So his solution is wrong - and so is yours! :laugh:
 
Share this answer
 
Comments
Patrice T 4-Nov-17 7:10am    
I think 10 is included.
x<=10
OriginalGriff 4-Nov-17 7:25am    
It is - which makes for the first 6, not the first five... ;)
Patrice T 4-Nov-17 7:28am    
agreed first 6
Richard Deeming 6-Nov-17 13:36pm    
Negative numbers can be even too, so it really depends on your definition of "first"! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900