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