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