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