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