Practice Challenges3/15

FizzBuzz

Challenge: FizzBuzz 🎯

Why this matters: FizzBuzz is the classic test of whether you can combine loops, conditions, and modulo logic—tiny building blocks of almost every algorithm.

The most famous coding interview question!

Rules

For numbers 1 to n:

  • If divisible by 3: "Fizz"
  • If divisible by 5: "Buzz"
  • If divisible by both: "FizzBuzz"
  • Otherwise: the number as string

Example

fizzBuzz(15);
// Returns:
// ["1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", 
//  "11", "Fizz", "13", "14", "FizzBuzz"]

Key Insight

Check divisible by 15 (both) first!

if (i % 15 === 0) {
  // divisible by both 3 and 5
} else if (i % 3 === 0) {
  // divisible by 3
} else if (i % 5 === 0) {
  // divisible by 5
} else {
  // just the number
}

Your Turn!

Implement FizzBuzz.