Chapter 02 of 22Part I: Foundations of Digital Information
The Secret Mathematics of Everyday Numbers
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.
Printed book snippet (byte-identical) · 444 bytes · SHA-256: cd24051198c1…
Standalone runnable
Dependencies: None (standard ES2022 JavaScript). Runs directly in Node.js or the isolated browser sandbox and logs output.
function passesLuhn(text) {
if (!/^\d+$/.test(text)) return false;
let sum = 0;
let doubleNext = false;
for (let i = text.length - 1; i >= 0; i--) {
let digit = Number(text[i]);
if (doubleNext) {
digit *= 2;
if (digit > 9) digit -= 9;
}
sum += digit;
doubleNext = !doubleNext;
}
return sum % 10 === 0;
}
console.log(passesLuhn("79927398713")); // true
console.log(passesLuhn("79927398714")); // false