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