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