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