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