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