Skip to content

Instantly share code, notes, and snippets.

@smithamax
Last active September 6, 2021 07:24
Show Gist options
  • Save smithamax/716ab371c9b059a339e9aa6b3dfe30da to your computer and use it in GitHub Desktop.
Save smithamax/716ab371c9b059a339e9aa6b3dfe30da to your computer and use it in GitHub Desktop.
function stringProduct(n1, n2) {
return toString(toInt(n1) * toInt(n2))
}
const CHAR_0 = "0".charCodeAt(0) // 48
function toInt(str) {
return [...str].reverse().reduce((acc, c, i) => {
return acc + BigInt(c.charCodeAt(0) - CHAR_0) * (10n**BigInt(i))
}, 0n)
}
function toString(int) {
if (int === 0n) {
return "0";
}
let str = ""
while (int > 0n) {
str = String.fromCharCode(CHAR_0 + Number(int % 10n)) + str
int = int / 10n
}
return str
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment