Skip to content

Instantly share code, notes, and snippets.

@wrschneider
Created February 10, 2017 02:05
Show Gist options
  • Save wrschneider/fa77f55029e94c7494abf921e649349d to your computer and use it in GitHub Desktop.
Save wrschneider/fa77f55029e94c7494abf921e649349d to your computer and use it in GitHub Desktop.
FizzBuzz in ES6 with no loops
// first version
var fizzBuzzN = n => {
if (n % 15 == 0) return "FizzBuzz";
if (n % 3 == 0) return "Fizz";
if (n % 5 == 0) return "Buzz";
return n;
}
// second version -- single divisibility test, no repeat of 3 and 5 cases
// 3 and 5
var fizzBuzzN = n => ([[3,"Fizz"],[5,"Buzz"]]).reduce((s,x)=> n % x[0] == 0 ? s+x[1] : s, "") || n
[... Array(100).keys()].map(i => i+1).map(fizzBuzzN).join('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment