← Interview Prep
JavaScriptEasy

FizzBuzz

Write fizzBuzz(n) that returns an array of strings for the numbers 1 to n:

  • multiples of 3 → "Fizz"
  • multiples of 5 → "Buzz"
  • multiples of both → "FizzBuzz"
  • otherwise the number as a string

fizzBuzz(5) → ["1", "2", "Fizz", "4", "Buzz"]

🧪 Practice

Implement fizzBuzz(n). Think about the order of your checks.

JavaScript
Loading editor…

Output appears here. Press Run or Ctrl/⌘ + Enter.

Reveal model answer & explanation

What the interviewer is looking for

FizzBuzz filters out candidates who can't write a loop and a condition under pressure. Do it quickly and cleanly, then talk about it:

  • Order of checks: test 15 (or % 3 && % 5) before 3 and 5, or "FizzBuzz" never appears.
  • Complexity: O(n) time, O(n) space for the output.
  • Extensibility: a follow-up is often "now add 7 → Bazz". A table-driven version scales:
const rules = [[3, "Fizz"], [5, "Buzz"], [7, "Bazz"]];
const say = (i) => rules.filter(([d]) => i % d === 0).map(([, w]) => w).join("") || String(i);