Explorer
JavaScript

Write a function `fizzBuzz(n)` that returns an array from 1 to n where multiples of 3 are "Fizz", multiples of 5 are "Buzz", multiples of both are "FizzBuzz", and others are the number itself.

Problem Statement

<p>Write a function <code>fizzBuzz(n)</code> that returns an array from 1 to n where multiples of 3 are "Fizz", multiples of 5 are "Buzz", multiples of both are "FizzBuzz", and others are the number itself.</p>

Examples

Input: n = 5

Output: [1, 2, "Fizz", 4, "Buzz"]

Explanation: 3 is replaced with "Fizz" and 5 with "Buzz".

Input: n = 15

Output: [..., 14, "FizzBuzz"]

Explanation: Multiples of both 3 and 5 are replaced with "FizzBuzz".

Complexity

Time Complexity: O(N)

Space Complexity: O(1)

Hints

šŸ’” Hint 1: Use the modulo operator (%) to check divisibility. šŸ’” Hint 2: Check divisibility by 15 first (both 3 and 5), then 3, then 5. šŸ’” Hint 3: Loop from 1 to n and push the appropriate value into an array.

Editorial & Approach

Problem Overview & Intuition

To solve FizzBuzz, we consider the execution characteristics of JavaScript engines. Write a function `fizzBuzz(n)` that returns an array from 1 to n where multiples of 3 are "Fizz", multiples of 5 are "Buzz", multiples of both are "FizzBuzz", and others are the number itself. By utilizing idiomatic language constructs and clean algorithmic principles, we can accomplish this with optimal time and memory usage.

Step-by-Step Approach

  1. Understand Problem Contract: Identify input arguments, return type expectations, and edge cases (empty inputs, nullish values).
  2. Choose Core Mechanism: Use modern JavaScript patterns (use the modulo operator (%) to check divisibility).
  3. Implement Logic: Handle state and transformations efficiently (check divisibility by 15 first (both 3 and 5), then 3, then 5).
  4. Return Result: Ensure proper return format and preserve caller context if applicable.

Optimal Implementation (JavaScript)

function fizzBuzz(n) {
  const result = [];
  for (let i = 1; i <= n; i++) {
    if (i % 15 === 0) result.push("FizzBuzz");
    else if (i % 3 === 0) result.push("Fizz");
    else if (i % 5 === 0) result.push("Buzz");
    else result.push(i);
  }
  return result;
}

Complexity Analysis

Time Complexity O(N) linear scan over input
Space Complexity O(1) constant auxiliary space (or O(N) output)

Edge Cases & Corner Traps Handled

  • Empty or boundary inputs (empty arrays, strings, zero length).
  • Type checks and unexpected values (e.g. null, undefined, negative numbers).
  • Closure preservation and memory isolation between separate invocations.

FizzBuzz

Easy

Write a function fizzBuzz(n) that returns an array from 1 to n where multiples of 3 are "Fizz", multiples of 5 are "Buzz", multiples of both are "FizzBuzz", and others are the number itself.

Example Scenarios
1Example 1
Input: n = 5
Output: [1, 2, "Fizz", 4, "Buzz"]
Explanation:

3 is replaced with "Fizz" and 5 with "Buzz".

2Example 2
Input: n = 15
Output: [..., 14, "FizzBuzz"]
Explanation:

Multiples of both 3 and 5 are replaced with "FizzBuzz".

Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
[1,2,"Fizz",4,"Buzz","Fizz",7,8,"Fizz","Buzz",11,"Fizz",13,14,"FizzBuzz"]