Project: FizzBuzz
JavaScript · Unit 3: Loops
- Full plan
- JavaScript
- Project
- 3 exercises
FizzBuzz is an interview classic: you count from 1, but the multiples of 3 say Fizz, the ones of 5 say Buzz and the ones of both say FizzBuzz.
Watch out: 15 is a multiple of 3 and of 5, so the order of the conditions matters.
const n = 15;
console.log(n % 3 === 0);
console.log(n % 5 === 0);Prints
true true
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
This function asks about the multiples of 3 first. What does it print with 15?
2. Write the code
Write
fizzbuzz(n). Return "FizzBuzz" ifnis a multiple of 3 and of 5, "Fizz" if only of 3, "Buzz" if only of 5 and, if not, the number as text withString(n).3. Write the code
You already have the function. Now the person types how far to play. Use a loop to print
fizzbuzzof each number, from 1 up to that number included.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.