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