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