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