Skip to content

Commit c81c499

Browse files
committed
Add solution #412
1 parent 90f6506 commit c81c499

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
383|[Ransom Note](./0383-ransom-note.js)|Easy|
165165
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
166166
405|[Convert a Number to Hexadecimal](./0405-convert-a-number-to-hexadecimal.js)|Easy|
167+
412|[Fizz Buzz](./0412-fizz-buzz.js)|Easy|
167168
414|[Third Maximum Number](./0414-third-maximum-number.js)|Easy|
168169
419|[Battleships in a Board](./0419-battleships-in-a-board.js)|Medium|
169170
442|[Find All Duplicates in an Array](./0442-find-all-duplicates-in-an-array.js)|Medium|

solutions/0412-fizz-buzz.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 412. Fizz Buzz
3+
* https://leetcode.com/problems/fizz-buzz/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer n, return a string array answer (1-indexed) where:
7+
* - answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
8+
* - answer[i] == "Fizz" if i is divisible by 3.
9+
* - answer[i] == "Buzz" if i is divisible by 5.
10+
* - answer[i] == i (as a string) if none of the above conditions are true.
11+
*/
12+
13+
/**
14+
* @param {number} n
15+
* @return {string[]}
16+
*/
17+
var fizzBuzz = function(n) {
18+
return Array.from(new Array(n), (_, i) => i + 1).map(i =>
19+
i % 3 === 0 && i % 5 === 0
20+
? 'FizzBuzz' : i % 3 === 0 ? 'Fizz' : i % 5 === 0 ? 'Buzz' : String(i)
21+
);
22+
};

0 commit comments

Comments
 (0)