Created
February 10, 2017 02:05
-
-
Save wrschneider/fa77f55029e94c7494abf921e649349d to your computer and use it in GitHub Desktop.
FizzBuzz in ES6 with no loops
This file contains 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
// 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