Read n and count from 1 to n, replacing some numbers with words.
What to build
The classic counting game: a loop with a decision inside it.
Requirements
- Print the numbers from 1 to n, one per line.
- Print
Fizzinstead of multiples of 3. - Print
Buzzinstead of multiples of 5. - Print
FizzBuzzinstead of multiples of both.
Input
One whole number n.
Output
n lines.
Constraints
n is from 1 to 100.
Example
Input:
5
Output:
1
2
Fizz
4
Buzz
Why it works
The test for both must come first. If i % 3 === 0 ran first, 15 would print Fizz and never reach the other branch, because only the first true branch of a chain runs.
Examples
Example 1 · Input
5
Expected output
1 2 Fizz 4 Buzz
Example 2 · Input
15
Expected output
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
Hints
Hint 1
Loop with for (let i = 1; i <= n; i++).
Hint 2
Test the case divisible by both 3 and 5 first.
Hint 3
i % 15 === 0 means divisible by both 3 and 5.
Solution
Show one correct solution (try it yourself first)
const n = Number(prompt());
for (let i = 1; i <= n; i++) {
if (i % 15 === 0) console.log("FizzBuzz");
else if (i % 3 === 0) console.log("Fizz");
else if (i % 5 === 0) console.log("Buzz");
else console.log(i);
}