Chapter 10 of 22 Part II: Networks and Communication

Finding Yourself on Planet Earth

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

Range Km (Ch. 10 Example 1)

File: block-01.js (byte-identical) · 245 bytes · SHA-256: 578275c70afd…
GPS Standalone runnable

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

const c = 299792458;
function rangeKm(milliseconds) {
  return c * milliseconds / 1e6;
}
function rangeErrorM(microseconds) {
  return c * microseconds / 1e6;
}
console.log(rangeKm(70));
console.log(rangeErrorM(1));
console.log(rangeErrorM(38));
Download .js

Locate (Ch. 10 Example 2)

File: block-02.js (byte-identical) · 415 bytes · SHA-256: fa8f06d325e1…
GPS 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], [4,8,5]];
function locate(beacons) {
  let best = { score: Infinity };
  for (let x = -2; x <= 10; x++) {
    for (let y = -5; y <= 10; y++) {
      let score = 0;
      for (const [a,b,r] of beacons) {
        const e = Math.hypot(x-a,y-b)-r;
        score += e*e;
      }
      if (score < best.score) best = {x,y,score};
    }
  }
  return best;
}
console.log(locate(beacons));
Download .js

Fit (Ch. 10 Example 3)

File: block-03.js (byte-identical) · 467 bytes · SHA-256: 39a7d3b65e5a…
GPS Standalone runnable

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

const observations = [[0,0,7],[8,0,7],[4,8,7]];
function fit(rows) {
  let best = {score: Infinity};
  for (let x = 0; x <= 8; x++) {
    for (let y = 0; y <= 8; y++) {
      for (let b = -3; b <= 3; b++) {
        let score = 0;
        for (const [a,d,r] of rows) {
          const e = Math.hypot(x-a,y-d)+b-r;
          score += e*e;
        }
        if (score < best.score) best = {x,y,b,score};
      }
    }
  }
  return best;
}
console.log(fit(observations));
Download .js