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