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