Skip to content

Commit b273ab8

Browse files
authored
Create truth-table.js
1 parent 8a7b4c2 commit b273ab8

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

truth-table.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const vals = [
2+
["-Infinity", -Infinity ],
3+
["-1", -1 ],
4+
['"-1"', "-1"],
5+
['""', ""],
6+
["[[]]", [[]] ],
7+
["[]", [] ],
8+
["false", false ],
9+
["0", 0 ],
10+
["null", null ],
11+
['"0"', "0" ],
12+
["[0]", [0] ],
13+
["[1]", [1] ],
14+
['"1"', "1" ],
15+
["1", 1 ],
16+
["true", true ],
17+
["Infinity", Infinity ],
18+
["{}", {} ],
19+
['"false"', "false" ],
20+
['"true"', "true" ],
21+
["undefined", undefined ],
22+
["NaN", NaN ],
23+
["\\t\\r\\n", "\t\r\n"]
24+
];
25+
26+
const cmp = (v1, v2) => v1 == v2;
27+
const canvas = document.getElementById("drawCanvas");
28+
const ctx = canvas.getContext("2d");
29+
const n = vals.length;
30+
const r = 20; // diameter of grid squares
31+
const p = 60; // padding space for labels
32+
const f = 12; // font size
33+
34+
// color grid cells
35+
for (let i = 0; i < n; i++) {
36+
let v1 = vals[i][1];
37+
for (let j = 0; j < n; j++) {
38+
let v2 = vals[j][1],
39+
eq = cmp(v1, v2);
40+
ctx.fillStyle = eq ? "orange" : "white";
41+
ctx.fillRect(p+i*r,p+j*r,r,r);
42+
}
43+
}
44+
45+
// draw labels
46+
ctx.fillStyle = "black";
47+
ctx.font = f + "px Helvetica";
48+
49+
for (let i = 0; i < n; i++) {
50+
let s = vals[i][0],
51+
w = ctx.measureText(s).width;
52+
ctx.save();
53+
ctx.translate(p+i*r+r/2-f*0.4,p-w-2);
54+
ctx.rotate(3.14159/2);
55+
ctx.fillText(s, 0, 0);
56+
ctx.restore();
57+
}
58+
59+
for (let i = 0; i < n; i++) {
60+
let s = vals[i][0],
61+
w = ctx.measureText(s).width;
62+
ctx.fillText(s, p-w-2, p+i*r+r/2+f*0.4);
63+
}
64+
65+
// draw grid lines
66+
ctx.beginPath();
67+
ctx.strokeStyle = "black";
68+
for (let i = 0; i <= n; i++) {
69+
ctx.moveTo(p+r*i, p);
70+
ctx.lineTo(p+r*i, p+r*n);
71+
ctx.moveTo(p, p+r*i);
72+
ctx.lineTo(p+r*n, p+r*i);
73+
}
74+
ctx.stroke();

0 commit comments

Comments
 (0)