From 257b2bedc3f00c17749762e81337c16e93fe9f80 Mon Sep 17 00:00:00 2001 From: zhudui <1209300584@qq.com> Date: Sun, 21 Aug 2022 11:43:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/add.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/add.js b/lib/add.js index 1714b95..1bbb725 100644 --- a/lib/add.js +++ b/lib/add.js @@ -1,5 +1,18 @@ -function add() { - // 实现该函数 +function add(a, b) { + const maxLength = Math.max(a.length, b.length); + const num1 = a.padStart(maxLength, '0'); + const num2 = b.padStart(maxLength, '0'); + let res = ''; + let carry = 0; + for (let i = num1.length - 1; i >= 0; --i) { + const count = parseInt(num1[i]) + parseInt(num2[i]) + carry; + carry = Math.floor(count / 10); + res = count % 10 + res; + } + if (carry) { + res = '1' + res; + } + return res; } module.exports = add \ No newline at end of file