Skip to content

Instantly share code, notes, and snippets.

@st98
Last active December 30, 2015 16:39
Show Gist options
  • Select an option

  • Save st98/7856002 to your computer and use it in GitHub Desktop.

Select an option

Save st98/7856002 to your computer and use it in GitHub Desktop.
Number#toStringを利用したFizzBuzz。
(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