Skip to content

Instantly share code, notes, and snippets.

@animatedlew
Created December 3, 2019 02:14
Show Gist options
  • Save animatedlew/763a8b9ba1992458184a0e1ffd0e0411 to your computer and use it in GitHub Desktop.
Save animatedlew/763a8b9ba1992458184a0e1ffd0e0411 to your computer and use it in GitHub Desktop.
function modulus(xs) {
let output = [];
while (true) {
if (xs.length) {
let [a, b] = [xs.shift(), xs.shift()];
if (b > a) output.push(a);
else {
let c = b;
while (true) {
let d = a - c;
if (d === 0) {
output.push(0);
break;
} else if (d < 0) {
c -= b;
output.push(a - c);
break;
}
c += b;
}
}
} else break;
}
return output;
}
console.log(modulus([8, 3, 6, 6, 4, 2, 3, 7]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment