Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: