Last active
December 30, 2015 16:39
-
-
Save st98/7856002 to your computer and use it in GitHub Desktop.
Number#toStringを利用したFizzBuzz。
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
| (function () { | |
| var isFizz = function (n) { | |
| var res = n.toString(3); | |
| res = res[res.length - 1] === '0'; | |
| return res; | |
| }; | |
| var isBuzz = function (n) { | |
| var res = n.toString(5); | |
| res = res[res.length - 1] === '0'; | |
| return res; | |
| }; | |
| var doFizzBuzz = function (a, b) { | |
| var res = []; | |
| if (typeof b === 'undefined') { | |
| b = a; | |
| a = 1; | |
| } | |
| for (; a <= b; a++) { | |
| res.push( | |
| [a, 'Fizz', 'Buzz', 'FizzBuzz'][isFizz(a) | isBuzz(a) << 1] | |
| ); | |
| } | |
| return res; | |
| }; | |
| console.log(doFizzBuzz(1, 100).join(', ')); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment