Skip to content

Instantly share code, notes, and snippets.

@goodforenergy
Created November 15, 2019 16:24
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 goodforenergy/473a6c0ddecd43b75d2371cb9bb6936d to your computer and use it in GitHub Desktop.
Save goodforenergy/473a6c0ddecd43b75d2371cb9bb6936d to your computer and use it in GitHub Desktop.
Magic 8 Set
/*
Usage:
const mySet = new Set([1, 2, 3]);
mySet.has(3); // 'It is decidedly so'
*/
const RESPONSES = {
YES: ['It is certain', 'It is decidedly so', 'Without a doubt', 'Yes - definitely', 'You may rely on it', 'As I see it, yes', 'Most likely', 'Outlook good', 'Yes', 'Signs point to yes'],
MAYBE: ['Reply hazy, try again', 'Ask again later', 'Better not tell you now', 'Cannot predict now', 'Concentrate and ask again'],
NO: ['Don\'t count on it', 'My reply is no', 'My sources say no', 'Outlook not so good', 'Very doubtful']
};
function getRandomInRange(max) {
return Math.floor(Math.random() * max);
}
Set.prototype.has = (function() {
const origHas = Set.prototype.has;
return function(item) {
const ran = Math.random();
let responseSet;
if (ran < 0.33) {
responseSet = RESPONSES.MAYBE;
} else {
responseSet = origHas.call(this, item) ? RESPONSES.YES : RESPONSES.NO;
}
return responseSet[getRandomInRange(responseSet.length)];
};
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment