Boss lesson · Unit 02
FizzBuzz and friends
Loops and conditions together: FizzBuzz, digit sums, a prime test and the off-by-one
What you practise
- Loops and conditions together: FizzBuzz, summing digits, testing for a prime, the off-by-one error found and fixed
Try a question from this lesson
Pick one
FizzBuzz: count from 1. Say Fizz for a multiple of 3, Buzz for a multiple of 5, FizzBuzz for a multiple of both, else the number. What is said at 15?
Correct answer
FizzBuzz
Why
15 is 3 × 5, so it is a multiple of both. FizzBuzz is the most famous small exercise in programming, because it needs a loop, several conditions, and some care about their order.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Is it prime?
A prime is a whole number above 1 that nothing divides exactly except 1 and itself: 2, 3, 5, 7, 11.
To test n, try every possible divisor d from 2 up to n - 1. If one of them leaves no remainder, n is not prime.
The variable prime is a flag: it starts as true and one failed test switches it off.
const n = 9;
let prime = true;
for (let d = 2; d < n; d++) {
if (n % d === 0) {
prime = false;
break;
}
}
console.log(prime); // false11 more exercises in the app
- 4× type the answer
- 4× multiple choice
- 1× fill the blanks
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.