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