Skip to content

Instantly share code, notes, and snippets.

@smithcommajoseph
Last active February 23, 2021 16:12
Show Gist options
  • Save smithcommajoseph/d260125e533cdc7f52b6402878d7eb95 to your computer and use it in GitHub Desktop.
Save smithcommajoseph/d260125e533cdc7f52b6402878d7eb95 to your computer and use it in GitHub Desktop.
// truth table POC
let vars = ['x1', 'x2', 'x3', 'x4'],
r = Math.pow(2, vars.length),
str = '',
i,
j;
str += `\r\n\r\n`
for (i = 0; i < vars.length; i++) {
str += `${vars[i]}\t`
}
str += `\r\n\r\n`
for (i = 0; i < r; i++) {
for (j = 0; j < vars.length; j++) {
let tmp = parseInt(
i
/ parseInt(
(r / Math.pow(2, j))
/ 2
)
) % 2;
str += `${tmp}\t`
}
str += `\r\n`
}
console.log(str);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment