Skip to content

Instantly share code, notes, and snippets.

@MartinMuzatko
Last active May 9, 2017 09:34
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 MartinMuzatko/61cc4505ace41b1c24b4b05debb7b5ce to your computer and use it in GitHub Desktop.
Save MartinMuzatko/61cc4505ace41b1c24b4b05debb7b5ce to your computer and use it in GitHub Desktop.
Truth Table
// usage : truthTable((a,b)=>a&&b, 2)
// 0,0 - 0
// 0,1 - 0
// 1,0 - 0
// 1,1 - 1
function truthTable(question, argcount) {
var combinations = binaryCombos(argcount)
for (var combination in combinations) {
combination = combinations[combination].reverse()
console.log(combination.toString()+' - ' + +!!truth(question, combination))
}
}
function truth(question,args) {
return question(...args)
}
function binaryCombos(n) {
var result = [];
for(y=0; y<Math.pow(2,n); y++){
var combo = [];
for(x=0; x<n; x++){
//shift bit and and it with 1
if((y >> x) & 1)
combo.push(1);
else
combo.push(0);
}
result.push(combo);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment