Skill-
Forge
Learn
Notes
Language & Framework notes
CS Core Subjects
OS, DBMS, Networks, OOP & more
System Design
Architecture & High-Scale
Roadmaps
Guided developer learning paths
Cheat Sheets
Quick syntax references
Resources
Curated books, guides & links
Practice
Problems
DSA & coding challenges
Quizzes
Test your knowledge
Algorithms
Explanations & visualizations
Git Visualizer
Interactive Git graph & CLI playground
Aptitude
Placement & logic prep
Formula Simulators
Real-time quant equation simulators
Build
Projects Hub
Step-by-step real world projects
Resume Builder
ATS-friendly resumes, live preview & score
Tools
Online compilers & utilities
Skills
Core professional & tech skills
Error Encyclopedia
Search
⌘K
Install App
Start Learning
Menu
Learn
Notes
CS Core Subjects
System Design
Roadmaps
Cheat Sheets
Resources
Practice
Problems
Quizzes
Algorithms
Git Visualizer
Aptitude
Formula Simulators
Build
Projects Hub
Resume Builder
Tools
Skills
Error Encyclopedia
Search
Install Skill-Forge App
Appearance
Start Learning
Skip to main content
Back to JavaScript Problems
easy
Math
Strings
Simulation
FizzBuzz
Est. Time:
5 min
Time:
O(n)
Space:
O(1)
Description
Given an integer n, return a string array where for each number from 1 to n: if divisible by 3, add "Fizz"; if divisible by 5, add "Buzz"; if divisible by both, add "FizzBuzz"; otherwise add the number as a string.
Examples
Example 1 Input:
n = 5
Output:
["1","2","Fizz","4","Buzz"]
Example 2 Input:
n = 15
Output:
["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz"]
Constraints
▪
1 <= n <= 10^4
Hints
Hint 1
Show
Check divisibility by 15 first (both 3 and 5), then by 3, then by 5.
Starter Code
Python
JavaScript
Java
C++
Solution.js
Copy
1
function fizzBuzz(n) {
2
// Your code here
3
}
Solution
Show Solution
Solve the problem first before reviewing the solution!
Reveal Solution
Up Next
Longest Substring Without Repeating Characters