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.
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 encode4(d) {
if (!/^[01]{4}$/.test(d)) throw Error('Enter four bits');
const [a,b,c,e] = [...d].map(Number);
return [a^b^e, a^c^e, a, b^c^e, b, c, e];
}
function syndrome(w) {
const s1 = w[0]^w[2]^w[4]^w[6];
const s2 = w[1]^w[2]^w[5]^w[6];
const s4 = w[3]^w[4]^w[5]^w[6];
return s1 + 2*s2 + 4*s4;
}
function decode7(received) {
const w = [...received];
const s = syndrome(w);
if (s) w[s-1] ^= 1;
return {position:s, word:w, data:[w[2],w[4],w[5],w[6]].join('')};
}
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 xorAll(values) {
return values.reduce((p,v)=>p^v,0);
}
function makeStripe(a,b,parityDisk) {
const stripe = [a,b];
stripe.splice(parityDisk,0,a^b);
return stripe;
}
function rebuild(stripe) {
const missing = stripe.filter(v=>v===null).length;
if (missing !== 1) throw Error('Exactly one missing block required');
return stripe.map(v=>v===null?xorAll(stripe.filter(x=>x!==null)):v);
}
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).