JavaScript Worksheet: If and For Loops
Code tip (Check, then count): if (condition) { … } else { … }: only one of the two parts runs. for (let i = 0; i < 5; i++) runs for i = 0, 1, 2, 3, 4 — 5 times. With i <= n it includes n; with i = i + 2 it goes up in twos.
NameDateTime takenScore ___ / 40
- 1.🔁 Which loop repeats code a set number of times? (a) else (b) if (c) let (d) for
-
2.🔁 What does this code print?
let age = 2;
if (age > 43) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
3.🔁 How many numbers does this code print?
for (let i = 0; i < 8; i = i + 3) {
console.log(i);
} -
4.🔁 What does this code print?
let score = 15;
if (score < 20) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
5.🔁 How many times does this code print “Welcome”?
for (let i = 3; i < 6; i++) {
console.log("Welcome");
} -
6.🔁 How many times does this code print “Welcome”?
for (let i = 2; i <= 7; i++) {
console.log("Welcome");
} - 7.🔁 Which word runs code only when something is true? (a) let (b) log (c) for (d) if
-
8.🔁 How many numbers does this code print?
for (let i = 0; i < 16; i = i + 4) {
console.log(i);
} -
9.🔁 What does this code print?
let marks = 21;
if (marks > 37) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
10.🔁 How many times does this code print “Hello”?
for (let i = 1; i <= 8; i++) {
console.log("Hello");
} - 11.🔁 When does the else part run? (a) always (b) never (c) when if is true (d) when if is false
-
12.🔁 How many times does this code print “Welcome”?
for (let i = 0; i < 3; i++) {
console.log("Welcome");
} -
13.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + i;
}
console.log(total); -
14.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + i;
}
console.log(total); -
15.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + 3;
}
console.log(total); - 16.🔁 What does i++ do? (a) doubles i (b) prints i (c) takes 1 from i (d) adds 1 to i
-
17.🔁 What does this code print?
let temp = 12;
if (temp > 12) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Cool (b) Hot -
18.🔁 What is the LAST number this code prints?
for (let i = 0; i < 19; i = i + 3) {
console.log(i);
} -
19.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 4; i++) {
total = total + 6;
}
console.log(total); -
20.🔁 What is the LAST number this code prints?
for (let i = 0; i < 13; i = i + 3) {
console.log(i);
} -
21.🔁 How many times does this code print “Hello”?
for (let i = 0; i <= 5; i++) {
console.log("Hello");
} -
22.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 7; i++) {
total = total + 6;
}
console.log(total); -
23.🔁 What does this code print?
let score = 30;
if (score >= 27) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
24.🔁 What does this code print?
let temp = 17;
if (temp >= 9) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
25.🔁 What does this code print?
let score = 2;
if (score >= 37) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again -
26.🔁 What does this code print?
let score = 10;
if (score < 25) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
27.🔁 How many times does this code print “Welcome”?
for (let i = 1; i < 10; i++) {
console.log("Welcome");
} -
28.🔁 How many times does this code print “Well done”?
for (let i = 1; i < 8; i++) {
console.log("Well done");
} -
29.🔁 How many numbers does this code print?
for (let i = 0; i < 15; i = i + 2) {
console.log(i);
} -
30.🔁 What does this code print?
let score = 46;
if (score >= 19) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again -
31.🔁 What is the LAST number this code prints?
for (let i = 0; i < 12; i = i + 4) {
console.log(i);
} -
32.🔁 What does this code print?
let temp = 33;
if (temp < 8) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
33.🔁 What is the LAST number this code prints?
for (let i = 0; i < 6; i = i + 2) {
console.log(i);
} -
34.🔁 How many numbers does this code print?
for (let i = 0; i < 10; i = i + 3) {
console.log(i);
} -
35.🔁 How many times does this code print “Hello”?
for (let i = 2; i < 4; i++) {
console.log("Hello");
} -
36.🔁 How many numbers does this code print?
for (let i = 0; i < 12; i = i + 3) {
console.log(i);
} -
37.🔁 How many times does this code print “Hello”?
for (let i = 1; i < 6; i++) {
console.log("Hello");
} -
38.🔁 How many times does this code print “Well done”?
for (let i = 1; i <= 8; i++) {
console.log("Well done");
} -
39.🔁 How many numbers does this code print?
for (let i = 0; i < 12; i = i + 4) {
console.log(i);
} -
40.🔁 How many numbers does this code print?
for (let i = 0; i < 18; i = i + 4) {
console.log(i);
}
Answer key
- for
- child
- 3
- You win
- 3
- 6
- if
- 4
- Try again
- 8
- when if is false
- 3
- 55
- 10
- 30
- adds 1 to i
- Cool
- 18
- 24
- 12
- 6
- 42
- You win
- Hot
- Play again
- You win
- 9
- 7
- 8
- You win
- 8
- Cool
- 4
- 4
- 2
- 4
- 5
- 8
- 3
- 5
Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids