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