Created
October 2, 2021 15:43
-
-
Save ashramwen/7bd032cb5e8db8d9b30621f09d075420 to your computer and use it in GitHub Desktop.
fizz buzz
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} n | |
| * @return {string[]} | |
| */ | |
| var fizzBuzz = function (n) { | |
| const output = []; | |
| // 題目明白寫了 1-indexed,所以 i 從 1 開始 | |
| for (let i = 1; i <= n; i++) { | |
| if (i % 15 === 0) { | |
| // 先處理 %3 和 %5 的部分,才不會重複處理 | |
| output.push('FizzBuzz'); | |
| } else if (i % 5 === 0) { | |
| output.push('Buzz'); | |
| } else if (i % 3 === 0) { | |
| output.push('Fizz'); | |
| } else { | |
| // 無法被 3 或 5 整除就輸出數字 (轉字串) | |
| output.push(String(i)); | |
| } | |
| } | |
| return output; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number} n | |
| * @return {string[]} | |
| */ | |
| var fizzBuzz = function (n) { | |
| const output = []; | |
| // 題目明白寫了 1-indexed,所以 i 從 1 開始 | |
| for (let i = 1; i <= n; i++) { | |
| if (i % 15 === 0) { | |
| // 先處理 %3 和 %5 的部分,才不會重複處理 | |
| output.push('FizzBuzz'); | |
| // 符合條件後直接跳到下個迭代 | |
| continue; | |
| } | |
| if (i % 5 === 0) { | |
| output.push('Buzz'); | |
| continue; | |
| } | |
| if (i % 3 === 0) { | |
| output.push('Fizz'); | |
| continue; | |
| } | |
| // 無法被 3 或 5 整除就輸出數字 (轉字串) | |
| output.push(String(i)); | |
| } | |
| return output; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment