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