Created
November 15, 2019 16:24
-
-
Save goodforenergy/473a6c0ddecd43b75d2371cb9bb6936d to your computer and use it in GitHub Desktop.
Magic 8 Set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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