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