Skip to content

Commit 26220ac

Browse files
412 Fizz Buzz
1 parent 3be15b8 commit 26220ac

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Diff for: 412. Fizz Buzz/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Write a program that outputs the string representation of numbers from 1 to n.
2+
// But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”.For numbers which are multiples of both three and five output “FizzBuzz”.
3+
// Example:
4+
5+
// n = 15,
6+
7+
// Return:
8+
// [
9+
// "1",
10+
// "2",
11+
// "Fizz",
12+
// "4",
13+
// "Buzz",
14+
// "Fizz",
15+
// "7",
16+
// "8",
17+
// "Fizz",
18+
// "Buzz",
19+
// "11",
20+
// "Fizz",
21+
// "13",
22+
// "14",
23+
// "FizzBuzz"
24+
// ]
25+
26+
//Solution
27+
28+
const fizzBuzz = (n) => {
29+
const result = [];
30+
31+
const stringPool = { 3: "Fizz", 5: "Buzz", 8: "FizzBuzz" }
32+
33+
for (var i = 1; i <= n; i++) {
34+
35+
let count = 0;
36+
if (i % 3 == 0) count += 3;
37+
if (i % 5 == 0) count += 5;
38+
39+
result.push(count == 0 ? i + "" : stringPool[count]);
40+
}
41+
return result
42+
};

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
| # | Title | Solutions | Difficulty |
66
|-----|----------------|---------------|-------------|
7-
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/1122.%Relative%Sort%Array/index.js)|Easy|
7+
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/1122.%Relative%Sort%Array/index.js)|Easy|
8+
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[Solution](../master/412.%Fizz%Buzz/index.js)|Easy|

0 commit comments

Comments
 (0)