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