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