Created
December 3, 2019 02:14
-
-
Save animatedlew/763a8b9ba1992458184a0e1ffd0e0411 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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