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