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