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