🟨 JavaScript
Easy
🕐 5 min

FizzBuzz Function

Goal: create a function fizzBuzz() that returns an array of 100 elements: multiples of 3 are replaced with “Fizz”, multiples of 5 with “Buzz”, and multiples of both 3 and 5 with “FizzBuzz”.

💡 Solution hint
  1. Check for multiples of 15 first, then 3 and 5.
  2. Build the result array and return it.
👀 Solution #1 (classic)
/**
 * FizzBuzz function - a classic divisibility test problem.
 * Returns an array where numbers divisible by 3 are replaced with "Fizz",
 * divisible by 5 with "Buzz", and divisible by both 3 and 5 (i.e., 15) with "FizzBuzz".
 * @returns {Array} Result array following FizzBuzz rules
 */
function fizzBuzz() {
  // Initialize array to store results
  const result = [];
  
  // Iterate through numbers 1 to 100 inclusive
  for (let i = 1; i <= 100; i++) {
    // Check divisibility by 15 first (to prevent
    // triggering separate 3 or 5 conditions)
    if (i % 15 === 0) {
      result.push('FizzBuzz');  // Divisible by both 3 and 5
    } 
    // If not divisible by 15, check for 3
    else if (i % 3 === 0) {
      result.push('Fizz');  // Divisible only by 3
    } 
    // If not divisible by 15 or 3, check for 5
    else if (i % 5 === 0) {
      result.push('Buzz');  // Divisible only by 5
    } 
    // If none of the above conditions are met
    else {
      result.push(i);  // Add the number itself
    }
  }
  
  // Return the populated results array
  return result;
}

Why this way:

  • Clear and readable.
  • Easy to extend or change conditions.
  • Time complexity O(n).
👀 Solution #2 (string concatenation)
function fizzBuzz() {
  const result = [];
  for (let i = 1; i <= 100; i++) {
    let str = '';
    if (i % 3 === 0) str += 'Fizz';
    if (i % 5 === 0) str += 'Buzz';
    result.push(str || i);
  }
  return result;
}

Why this way:

  • No duplicate checks.
  • Easy to scale for more divisors (e.g., 7 → ‘Bazz’).
  • Also O(n).
👀 Solution #3 (Array.from + map)
function fizzBuzz() {
  return Array.from({ length: 100 }, (_, idx) => {
    const i = idx + 1;
    let str = '';
    if (i % 3 === 0) str += 'Fizz';
    if (i % 5 === 0) str += 'Buzz';
    return str || i;
  });
}

Why this way:

  • Functional style and compactness.
  • No external mutable state.
  • Explicit mapping of index to value.

Task description

Write a function fizzBuzz that returns an array of length 100 with replacements:

  • Numbers divisible by 3 are replaced with “Fizz”
  • Numbers divisible by 5 are replaced with “Buzz”
  • Numbers divisible by both 3 and 5 are replaced with “FizzBuzz”

Examples

fizzBuzz()[2]; // "Fizz" (number 3)
fizzBuzz()[4]; // "Buzz" (number 5)
fizzBuzz()[14]; // "FizzBuzz" (number 15)

Requirements

  • The function must be named fizzBuzz
  • Do not print to console, return the array instead
  • Work for the range 1–100

🧑‍💻 It's not a bug! It's a feature!

The code editor is intentionally hidden on mobile.

Believe me, it's for the best: I am protecting you from the temptation to code in less-than-ideal conditions. A small screen and a virtual keyboard are not the best tools for a programmer.

📖 Now: Study the task, think through the solution. Act like a strategist.

💻 Later: Sit down at your computer, open the site, and implement all your ideas comfortably. Act like a code-jedi!