Skip to content

Instantly share code, notes, and snippets.

@ChillyBwoy
Created January 20, 2016 13:55
Show Gist options
  • Save ChillyBwoy/9973d9eac3d165099f84 to your computer and use it in GitHub Desktop.
Save ChillyBwoy/9973d9eac3d165099f84 to your computer and use it in GitHub Desktop.
FizzBuzz
// with array. More more more faster
Array.apply(null, Array(100)).map((_, i) => {
var n = i + 1;
return (n % 3 === 0 && n % 5 === 0 ? 'FizzBuzz' :
(n % 3 === 0 ? 'Fizz' :
(n % 5 === 0 ? 'Buzz' : n)))
});
// with recursion
const fizzBuzz = (size, acc = []) => {
if (size <= 0) {
return acc;
}
let n = acc.length + 1;
let word = (n % 3 === 0 && n % 5 === 0 ? 'FizzBuzz' :
(n % 3 === 0 ? 'Fizz' :
(n % 5 === 0 ? 'Buzz' : n)));
return fizzBuzz(size - 1, [...acc, word]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment