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