본문 바로가기
개발 노트/LeetCode 풀이

[JS] 412. Fizz Buzz

by tokkiC 2022. 11. 16.

조건문 기본 문제

if 문을 어떻게 사용하냐에 따라 속도나 비용의 효율을 높일 수 있겠다

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

 

Fizz Buzz - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

/**
 * @param {number} n
 * @return {string[]}
 */
var fizzBuzz = function(n) {
  const ans = [];
  for (let i = 1; i <= n; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
      ans.push('FizzBuzz');
      continue;
    }
    if (i % 3 === 0) {
      ans.push('Fizz');
      continue;
    }
    if (i % 5 === 0) {
      ans.push('Buzz');
      continue;
    }
    ans.push(`${i}`);
  }
  return ans;
};

'개발 노트 > LeetCode 풀이' 카테고리의 다른 글

[JS] 1. Two Sum  (0) 2022.11.22
[JS] 383. Ransom Note  (0) 2022.11.18
[JS] 1342. Number of Steps to Reduce a Number to Zero  (0) 2022.11.18
[JS] 1672. Richest Customer Wealth  (0) 2022.11.16
[JS] 1480. Running Sum of 1d Array  (0) 2022.11.15

댓글