Last Updated: June 28, 2016
·
2.124K
· yitsushi

Yes or No, random answer from Node.js

Sometimes I need to decide something with a simple yes or no but I don't know what to do. My way is a simple JavaScript call:

Array.apply(null, new Array(1000)).map(Number.prototype.valueOf,0).map(function() { return Math.round(Math.random()*100000) % 2; }).reduce(function(carry, i) { if (i) { carry.yes++; } else { carry.no++; } return carry; }, {yes: 0, no: 0});

ok it's a little bit dirty so here it is:

// Build an array with 1000 zeros
Array.apply(null, new Array(1000))
  .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) % 2;
  })
  .reduce(
    function(carry, i) {
      // If one then it's a Yes
      // If zero then it's a No
      if (i) { carry.yes++; }
      else { carry.no++; }
      return carry;
    },
    {yes: 0, no: 0}
  );

Now I got the answer:

{ "yes": 506, "no": 494 }

Decision made!

Ok it's a simple "Coin Flipper" so improve it to handle more answers. First of all warp into a function with "infinite" parameters. Each parameter will be an answer, except the first. The first will be a number of samples.

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
    );
}

How to call?

console.log(iNeedAnswers());
console.log(iNeedAnswers(500));
console.log(iNeedAnswers(500, "Yes"));
console.log(iNeedAnswers(500, "Yes", "No"));
console.log(iNeedAnswers(500, "Yes", "No", "Maybe"));
console.log(iNeedAnswers("Yes", "No", "Maybe"));

Output?

{ "Yes": 515, "No": 485 }
{ "Yes": 252, "No": 248 }
{ "Yes": 240, "No": 260 }
{ "Yes": 242, "No": 258 }
{ "Yes": 155, "No": 170, "Maybe": 175 }
{ "Yes": 332, "No": 333, "Maybe": 335 }

Ok, we don't need to type this every time we need to decide something. Add this line to the end of the file:

console.log(iNeedAnswers.apply(this, process.argv.slice(2)));

And this to the top of the file (it will be the first line):

#!/usr/bin/env node

Now we have an awesome script that we can use like any other program on our system.

❯ chmod +x flip.js
❯ mv flip.js /usr/local/bin/myflip
❯ myflip
{ Yes: 468, No: 532 }
❯ myflip 5000 Yes No Maybe
{ Yes: 1679, No: 1640, Maybe: 1681 }
❯ myflip 5000 Yes No Maybe Tomorrow
{ Yes: 1225, No: 1259, Maybe: 1286, Tomorrow: 1230 }

So it says: No, don't do it, or if you really don't know then Maybe.

Full source code is available as a gist: https://gist.github.com/Yitsushi/1ad9fe8638e0a0010906