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