Skip to content

Instantly share code, notes, and snippets.

@PineappleRind
Created February 14, 2022 16:43
Show Gist options
  • Save PineappleRind/a8117f846cd779a1c3351e0a2c585e4e to your computer and use it in GitHub Desktop.
Save PineappleRind/a8117f846cd779a1c3351e0a2c585e4e to your computer and use it in GitHub Desktop.
Dynamic JavaScript function for hamming codes
// Implementation in JavaScript of the Python script outlined in 3Blue1Brown's video
// https://www.youtube.com/watch?v=X8jsijhllIA
function hamming(e) {
let t = [];
for (let n = 0; n < bits.length; n++) e[n] && t.push(n); // create array of the indices of only the bits that are 1
return (t = t.reduce((e, t) => e ^ t)) || false && t // reduce by bitwise XOR
}
// Function takes in bit array, and returns the bit index that is wrong, or nothing if nothing is wrong
// Haven't tested how well it scales
let bits = [
0, 1, 1, 1,
1, 1, 1, 0,
1, 1, 1, 0,
1, 1, 1, 1
]
console.log(hamming(bits))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment