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