Home / Volume 1 / Chapter 19 / Code Chapter 19 of 22 Part IV: Information Processing
When Machines Learn to See
Lesson & Field Guide
Interactive Lab
3 Projects
Source Code
Self-Check Quiz
4 Verified Book Snippets
Every .js file below is byte-identical (SHA-256 verified) to the printed code in Volume 1. Runnable blocks execute inside an isolated sandboxed iframe and Web Worker with a 2-second timeout and zero network access.
Download Chapter ZIP
Edges (Ch. 19 Example 1)
File: block-01.js (byte-identical) · 296 bytes · SHA-256: d3a301428fae…
edge detection Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).
function edges(grid, threshold = 80) {
return grid.map((row, y) => row.map((v, x) => {
const dx = x + 1 < row.length ?
Math.abs(v - row[x + 1]) : 0;
const dy = y + 1 < grid.length ?
Math.abs(v - grid[y + 1][x]) : 0;
return Math.max(dx, dy) >= threshold ? 1 : 0;
}));
} Sandboxed Output (Worker timeout: 2s, no network) Reset
[[0,1,0],[0,1,0]]
File: block-02.js (byte-identical) · 94 bytes · SHA-256: f825b00ad707…
edge detection Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.
const sample = [[20,20,200],[20,20,200]];
console.log(edges(sample, 80));
// [[0,1,0],[0,1,0]] Sandboxed Output (Worker timeout: 2s, no network) Reset
Recognize (Ch. 19 Example 3)
File: block-03.js (byte-identical) · 638 bytes · SHA-256: 998774decfa4…
edge detection Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).
const templates = [
'111101101101111', '010110010010111',
'111001111100111', '111001111001111',
'101101111001001', '111100111001111',
'111100111101111', '111001001001001',
'111101111101111', '111101111001111'
];
function recognize(bits) {
if (!/^[01]{15}$/.test(bits)) throw Error('15 bits needed');
const ranked = templates.map((t, digit) => ({
digit, errors: [...t].filter((b,i) => b !== bits[i]).length
})).sort((a,b) => a.errors - b.errors);
const best = ranked[0];
const tied = ranked[1].errors === best.errors;
return { digit: tied || best.errors > 2 ? null : best.digit,
errors: best.errors, tied };
} Sandboxed Output (Worker timeout: 2s, no network) Reset
Evaluate (Ch. 19 Example 4)
File: block-04.js (byte-identical) · 494 bytes · SHA-256: 1746cd178afd…
edge detection Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Defines pure JavaScript functions or data; runs cleanly in the sandbox (call console.log in a test harness to inspect values).
function evaluate(samples, threshold) {
const result = {tp:0, fp:0, tn:0, fn:0};
for (const {score, same} of samples) {
const accepted = score >= threshold;
if (same) result[accepted ? 'tp' : 'fn']++;
else result[accepted ? 'fp' : 'tn']++;
}
return result;
}
const samples = [
{score:.95,same:true}, {score:.88,same:true},
{score:.76,same:true}, {score:.61,same:true},
{score:.81,same:false}, {score:.64,same:false},
{score:.42,same:false}, {score:.22,same:false}
]; Sandboxed Output (Worker timeout: 2s, no network) Reset
← Ch. 18: Finding a Needle in a Digital Universe
Ch. 20: Inside the Mind of Artificial Intelligence →