Last active
July 24, 2021 09:54
-
-
Save sangkukbae12/0a4758cad1a3af1f36d0d2620b873041 to your computer and use it in GitHub Desktop.
test1
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
const args = process.argv.slice(2); | |
sumTwoNum(args[0], args[1]) | |
function sumTwoNum(str1, str2) { | |
if (str1.length > str2.length) { | |
[str1, str2] = [str2, str1] | |
} | |
let str = "" | |
let n1 = str1.length, n2 = str2.length; | |
str1 = str1.split("").reverse().join(""); | |
str2 = str2.split("").reverse().join(""); | |
let carry = 0; | |
for (let i = 0; i < n1; i++) { | |
let sum = ((str1[i].charCodeAt(0) - '0'.charCodeAt(0)) + (str2[i].charCodeAt(0) - '0'.charCodeAt(0)) + carry); | |
str += String.fromCharCode(sum % 10 + '0'.charCodeAt(0)); | |
carry = Math.floor(sum / 10); | |
} | |
for (let i = n1; i < n2; i++) { | |
let sum = ((str2[i].charCodeAt(0) - '0'.charCodeAt(0)) + carry); | |
str += String.fromCharCode(sum % 10 + '0'.charCodeAt(0)); | |
carry = Math.floor(sum / 10); | |
} | |
if (carry > 0) { | |
str += String.fromCharCode(carry + '0'.charCodeAt(0)); | |
} | |
str = str.split("").reverse().join(""); | |
console.log(str); | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment