Created
July 24, 2017 22:56
-
-
Save doug-numetric/bd84fc5d7816924910c74c86a2127979 to your computer and use it in GitHub Desktop.
ESLint friendly conditional expression
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
const branch = function(b) { | |
return b.if ? b.then : b.else; | |
} | |
const fizzbuzz = function(num) { | |
return branch({ | |
if: num % 3 === 0 && num % 5 === 0, | |
then: 'fizzbuzz', | |
else: branch({ | |
if: num % 3 === 0, | |
then: 'fizz', | |
else: branch({ | |
if: num % 5 === 0, | |
then: 'buzz', | |
else: num | |
}) | |
}) | |
}); | |
} | |
console.log( | |
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16].map( | |
cv => fizzbuzz(cv) | |
) | |
); | |
// [1, 2, "fizz", 4, "buzz", "fizz", 7, 8, "fizz", "buzz", 11, "fizz", 13, 14, "fizzbuzz", 16] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment