Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created October 8, 2014 23:23
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 yitsushi/1ad9fe8638e0a0010906 to your computer and use it in GitHub Desktop.
Save yitsushi/1ad9fe8638e0a0010906 to your computer and use it in GitHub Desktop.
Coderwall article: https://coderwall.com/p/ttbjjq
#!/usr/bin/env node
function iNeedAnswers() {
var args = Array.prototype.slice.call(arguments),
answerCount = 0,
answers;
if (args.length < 1) { sampleRate = 1000; }
else if (parseInt(args[0], 10) != args[0]) { sampleRate = 1000; }
else { sampleRate = parseInt(args.shift(), 10); }
if (args.length < 2) {
args = ["Yes", "No"];
}
answers = args.reduce(function(carry, answer) {
carry[answer] = 0;
return carry;
}, {});
answerCount = args.length;
// Build an array with 1000 zeros
return Array.apply(null, new Array(sampleRate))
.map(Number.prototype.valueOf, 0)
.map(function() {
// Fill up with zeros and ones
// based on random numbers (odd or even)
return Math.round(Math.random()*100000) % answerCount;
})
.reduce(
function(carry, i) {
carry[args[i]]++;
return carry;
},
answers
);
}
console.log(iNeedAnswers.apply(this, process.argv.slice(2)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment