Skip to content

Instantly share code, notes, and snippets.

@doug-numetric
Created July 24, 2017 22:56
Show Gist options
  • Save doug-numetric/bd84fc5d7816924910c74c86a2127979 to your computer and use it in GitHub Desktop.
Save doug-numetric/bd84fc5d7816924910c74c86a2127979 to your computer and use it in GitHub Desktop.
ESLint friendly conditional expression
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