Extra Practice
Remember: Learning to program takes practice! It helps to see concepts over and over, and it's always good to try things more than once. We learn much more the second time we do something. Use the exercises and additional self-checks below to practice.
1. Looping and Conditionals
let maxNum = 20;
for (let i=1; i<=maxNum; i++) {
if (i % 2 === 0) {
console.log(`${i} is even.`;
} else {
console.log(`${i} is odd.`);
}
}
What is the name of the counter variable in this for
loop?
maxNum
i
console
undefined
i
.How many times will this loop execute?
What is the starting value of i
?
i
is 1.What is the conditional statement checking for?
i
divided by 2
is 0
.maxNum
divided by 2
is 0
.i
divided by 2
is 0
.i
divided by 2
is even.i
divided by 2
is 0
.What happens to i
on each loop?
i
is increased by one on each iteration of the loop.What would the loop output when i
equals 7
?
"7 is even."
"7 is odd."
i
is equal to 7
, the console log would be "7 is odd."
.What would the loop output when the condition (i % 2 === 0)
equals false
?
`${i} is even.`
`${i} is odd.`
(i % 2 === 0)
equals false
, the console log would be `${i} is odd.`
.Visit Content Online
The content on this page has been removed from your PDF or ebook format. You may view the content by visiting this book online.