JavaScript solution for easier expanding the condition.


  • 0
    G

    For easier expanding the condition in var tuple,

    e.g. var tuple = { '3': 'Fizz',
                       '5': 'Buzz',
                       '7': 'Foo',
                      '19': 'Bar' };
    

    The solution:

    var fizzBuzz = function(n) {
        if (n <= 0) return '0';
        
        var tuple = {'3': 'Fizz', '5':'Buzz'};
        var s = [];
        
        for (var ii = 1; ii <= n; ii++) {
            var c = '';
            for (var jj in tuple) {
                if (ii % jj === 0) { c += tuple[jj]; }
            }
            if (c === '') { c = ii.toString(); }
            s.push(c);
        }
        
        return s;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.