Skip to main content

3 posts tagged with "simulation"

View All Tags

· One min read

https://leetcode.com/problems/fizz-buzz/

Solution 1

/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function (n) {
const result = [];

for (let i = 1; i <= n; i++) {
if (i % 15 === 0) {
result.push('FizzBuzz');
} else if (i % 5 === 0) {
result.push('Buzz');
} else if (i % 3 === 0) {
result.push('Fizz');
} else {
result.push(i.toString());
}
}

return result;
};

Runtime: 102 ms, faster than 64.62% of JavaScript online submissions for Fizz Buzz.

Memory Usage: 44.3 MB, less than 69.20% of JavaScript online submissions for Fizz Buzz.