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