Last active
April 27, 2024 05:42
-
-
Save MartinMuzatko/61cc4505ace41b1c24b4b05debb7b5ce to your computer and use it in GitHub Desktop.
Truth Table
This file contains hidden or 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 : 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