Last active
September 6, 2021 07:24
-
-
Save smithamax/716ab371c9b059a339e9aa6b3dfe30da to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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