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