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