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