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