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