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