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