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