News for hackers. Stuff that matters.
Re:Fizzbuzz, Interviews, And Overthinking by just2n on Thursday October 04, 2012 @06:46PM (#4614815)
I agree. Same thing in JS. Also, this is precisely what I thought to do when he started asking about 7 and 11.

    (function fizzbuzz(n, cases) {
      var i = 0;
      while (i++ < n) {
        console.log(cases.reduce(function (prev, c) {
          return i % c[0] ? prev : (prev || '') + c[1];
        }, null) || i);
      }
    }(100, [[3, 'Fizz'], [5, 'Buzz'], [7, 'Bazz'], [11, 'Boo'], [13, 'Blip']]));
Excuse the terseness, I didn't want to make this post too lengthy, but it's still quite readable, IMO. Array.reduce was designed to solve problems like this.

There are no comments on this comment yet.