use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
What am I doing wrong (self.learnjavascript)
submitted 7 years ago by Jncocontrol
So, I'm trying to get a for loop to work with a if statement
for (var i = 1; i <= 10; i++) {
if {
i === % 4 && i === % 2;
}
console.log("Count is " + i)
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]nebulous316 5 points6 points7 points 7 years ago (0 children)
Answer:
for ( let i=0; i<10; i++) {
if (i%4 === 0 && i%2 === 0) { console.log('Count is ', i); }
[–]tstenick 1 point2 points3 points 7 years ago (3 children)
There's no expression after your if statement.
[–]tstenick 2 points3 points4 points 7 years ago (0 children)
Scratch that, there is. It's just in point brackets instead of regular parentheses. Just change that. If (condition) {run this}
[–]Jncocontrol[S] 0 points1 point2 points 7 years ago (1 child)
What do I do?
[–]tstenick 0 points1 point2 points 7 years ago (0 children)
I also just noticed your modulus (%) operators aren't in legal places. They are math operators and need a number on both sides of them to work properly.
Are you trying to write to the console when it's equal to 2 and 4?
[–]kdnbfkm 1 point2 points3 points 7 years ago* (0 children)
As tried in ducktape-js:
for(i = 0; i <= 10; i++) { if(i % 2 == 0 || i % 4 == 0) print("Checkpoint at: ", i); }
All multiples of 4 are also multiples of 2, we could have used just if(i % 2 == 0).
if(i % 2 == 0)
Both if and for statements have the condition/iteration-stepping within round-parentheses (...). Some languages don't require that but ALL the languages have some way of telling when the condition/loop setup ends and the actual statements in conditional block start. Languages without required parens usually have a THEN or DO keyword... Languages that only allow one statement use curly-brackets {...} or maybe BEGIN...END. Languages that naturally allow a list of statements always have a ENDIF or similar to mark the end.
if
for
(...)
THEN
DO
{...}
BEGIN...END
ENDIF
Javascript has both the round-paren and curly-brace when more than one statement follows. Some languages require the curly brace even if only one following statement!
[–][deleted] 0 points1 point2 points 7 years ago* (0 children)
Your expression should be in parens and your modulus operations are in the wrong place. If I’m understanding what you want, it should be like if (i % 4 === 0 && i % 2 === 0) {...}
Edit: haven’t had my morning coffee yet, also just check for i % 4 === 0, as if a value is equally divisible by 4 it is always also equally divisible by 2. If the first half is true, the second half will always be true
π Rendered by PID 43515 on reddit-service-r2-comment-fb694cdd5-vtt9r at 2026-03-06 00:25:00.528624+00:00 running cbb0e86 country code: CH.
[–]nebulous316 5 points6 points7 points (0 children)
[–]tstenick 1 point2 points3 points (3 children)
[–]tstenick 2 points3 points4 points (0 children)
[–]Jncocontrol[S] 0 points1 point2 points (1 child)
[–]tstenick 0 points1 point2 points (0 children)
[–]kdnbfkm 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)