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