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.🔁 What does this code print?
let score = 21;
if (score >= 12) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
2.🔁 How many times does this code print “Well done”?
for (let i = 2; i <= 7; i++) {
console.log("Well done");
} -
3.🔁 What does this code print?
let age = 24;
if (age < 20) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
4.🔁 How many numbers does this code print?
for (let i = 0; i < 12; i = i + 4) {
console.log(i);
} -
5.🔁 What does this code print?
let marks = 39;
if (marks >= 24) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Try again (b) Pass - 6.🔁 What does i++ do? (a) takes 1 from i (b) doubles i (c) prints i (d) adds 1 to i
- 7.🔁 Which loop repeats code a set number of times? (a) for (b) let (c) if (d) else
-
8.🔁 How many times does this code print “Hello”?
for (let i = 0; i <= 9; i++) {
console.log("Hello");
} - 9.🔁 When does the else part run? (a) when if is true (b) always (c) when if is false (d) never
-
10.🔁 How many times does this code print “Welcome”?
for (let i = 1; i < 10; i++) {
console.log("Welcome");
} -
11.🔁 How many times does this code print “Hi”?
for (let i = 3; i <= 9; i++) {
console.log("Hi");
} -
12.🔁 What does this code print?
let age = 50;
if (age < 25) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen - 13.🔁 Which word runs code only when something is true? (a) if (b) for (c) log (d) let
-
14.🔁 How many numbers does this code print?
for (let i = 0; i < 15; i = i + 2) {
console.log(i);
} -
15.🔁 What does this code print?
let marks = 6;
if (marks >= 29) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Try again (b) Pass -
16.🔁 How many times does this code print “Hi”?
for (let i = 3; i < 5; i++) {
console.log("Hi");
} -
17.🔁 What does this code print?
let age = 37;
if (age >= 14) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
18.🔁 How many times does this code print “Hi”?
for (let i = 0; i < 8; i++) {
console.log("Hi");
} -
19.🔁 What does this code print?
let temp = 36;
if (temp < 18) {
console.log("Hot");
} else {
console.log("Cool");
} (a) Hot (b) Cool -
20.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 7; i++) {
total = total + i;
}
console.log(total); -
21.🔁 What does this code print?
let marks = 42;
if (marks >= 24) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
22.🔁 How many numbers does this code print?
for (let i = 0; i < 5; i = i + 3) {
console.log(i);
} -
23.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 7; i++) {
total = total + 7;
}
console.log(total); -
24.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + i;
}
console.log(total); -
25.🔁 What is the LAST number this code prints?
for (let i = 0; i < 8; i = i + 2) {
console.log(i);
} -
26.🔁 How many times does this code print “Well done”?
for (let i = 0; i < 3; i++) {
console.log("Well done");
} -
27.🔁 How many numbers does this code print?
for (let i = 0; i < 8; i = i + 3) {
console.log(i);
} -
28.🔁 What does this code print?
let age = 29;
if (age < 9) {
console.log("teen");
} else {
console.log("child");
} (a) teen (b) child -
29.🔁 What is the LAST number this code prints?
for (let i = 0; i < 17; i = i + 2) {
console.log(i);
} -
30.🔁 What does this code print?
let marks = 27;
if (marks < 24) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Pass (b) Try again -
31.🔁 What does this code print?
let score = 44;
if (score < 21) {
console.log("You win");
} else {
console.log("Play again");
} (a) You win (b) Play again -
32.🔁 What number does this code print?
let total = 0;
for (let i = 1; i <= 8; i++) {
total = total + i;
}
console.log(total); -
33.🔁 What does this code print?
let marks = 21;
if (marks >= 45) {
console.log("Pass");
} else {
console.log("Try again");
} (a) Try again (b) Pass -
34.🔁 How many times does this code print “Hello”?
for (let i = 3; i < 10; i++) {
console.log("Hello");
} -
35.🔁 What does this code print?
let age = 32;
if (age > 21) {
console.log("teen");
} else {
console.log("child");
} (a) child (b) teen -
36.🔁 How many times does this code print “Hi”?
for (let i = 3; i <= 7; i++) {
console.log("Hi");
} -
37.🔁 What is the LAST number this code prints?
for (let i = 0; i < 20; i = i + 4) {
console.log(i);
} -
38.🔁 What does this code print?
let score = 35;
if (score >= 40) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win -
39.🔁 How many times does this code print “Welcome”?
for (let i = 3; i < 6; i++) {
console.log("Welcome");
} -
40.🔁 What does this code print?
let score = 16;
if (score > 15) {
console.log("You win");
} else {
console.log("Play again");
} (a) Play again (b) You win
Answer key
- You win
- 6
- child
- 3
- Pass
- adds 1 to i
- for
- 10
- when if is false
- 9
- 7
- child
- if
- 8
- Try again
- 2
- teen
- 8
- Cool
- 28
- Pass
- 2
- 49
- 55
- 6
- 3
- 3
- child
- 16
- Try again
- Play again
- 36
- Try again
- 7
- teen
- 5
- 16
- Play again
- 3
- You win
Free html and css lessons, practice and worksheets at talentjr.in/html-css-for-kids