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