Skip to content

Instantly share code, notes, and snippets.

@lujb
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lujb/61b116b5061b5b750716 to your computer and use it in GitHub Desktop.
Save lujb/61b116b5061b5b750716 to your computer and use it in GitHub Desktop.
var _ = require('underscore');
var special_words = ["Fizz", "Buzz", "Whizz"];
var candidates = seq(9); // 1..9
// select 3 random numbers from candidates.
var special = _(3).times(function(i) {
var selected = candidates[_.random(candidates.length-1)];
candidates = _.without(candidates, selected);
return selected;
});
console.log(special);
var input = seq(100); // 1..100
// main
var output = _.map(input, function yamete(n) {
return rule1(n) || rule2(n) || rule3(n);
});
console.log(_.zip(input, output));
function rule1(n) {
return new RegExp(special[0] + '').test(n + '') ? special_words[0]:false;
}
function rule2(n) {
return _.reduce(special, function(memo, s){
this.i++;
return memo + ((n%s) ? '' : special_words[this.i]);
}, '', {i:-1});
}
function rule3(n) { return n; }
function seq(n) { return _(n).times(function(n){return n+1}); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment