Skip to content

Instantly share code, notes, and snippets.

@doug-numetric
Created July 24, 2017 22:56
Show Gist options
  • Select an option

  • Save doug-numetric/bd84fc5d7816924910c74c86a2127979 to your computer and use it in GitHub Desktop.

Select an option

Save doug-numetric/bd84fc5d7816924910c74c86a2127979 to your computer and use it in GitHub Desktop.

Revisions

  1. doug-numetric created this gist Jul 24, 2017.
    27 changes: 27 additions & 0 deletions conditionalExpression.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    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]