Chapter 07 of 22 Part II: Networks and Communication

The Digital Detective

3 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

Distance (Ch. 07 Example 1)

File: block-01.js (byte-identical) · 148 bytes · SHA-256: 3252ccc1b975…
cell tower Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

function distance(x, y, a, b) {
  return Math.hypot(x - a, y - b);
}
console.log(distance(3, 4, 0, 0)); // 5
console.log(distance(4, 3, 8, 0)); // 5
Download .js

[[4,-3],[4,3]]

File: block-02.js (byte-identical) · 481 bytes · SHA-256: 0fb037c8796e…
cell tower Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

const beacons = [[0, 0, 5], [8, 0, 5], [0, 6, 5]];
function candidates(count, tolerance) {
  const hits = [];
  for (let x = -5; x <= 13; x++) {
    for (let y = -5; y <= 11; y++) {
      const fits = beacons.slice(0, count).every(
        ([a,b,r]) => Math.abs(
          Math.hypot(x-a, y-b) - r
        ) <= tolerance
      );
      if (fits) hits.push([x,y]);
    }
  }
  return hits;
}
console.log(candidates(2, 0));
// [[4,-3],[4,3]]
console.log(candidates(3, 0)); // [[4,3]]
Download .js

Records — The Digital Detective (Example 3)

File: block-03.js (byte-identical) · 346 bytes · SHA-256: 2ef92df3c457…
cell tower Standalone runnable

Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated Web Worker sandbox and prints console output.

const records = [
  {time: "10:20", type: "cell", value: "B"},
  {time: "10:00", type: "cell", value: "A"},
  {time: "10:15", type: "attendance",
   value: "Owner reported in class"}
];
const ordered = [...records].sort((a,b) =>
  a.time.localeCompare(b.time));
for (const event of ordered) {
  console.log(event.time, event.type, event.value);
}
Download .js