实现大整数加法
即两个数字字符相加,采用按位加法实现
方法
const add = (a, b) => {let i = a.length - 1;let j = b.length - 1;let carry = 0;let ret = "";while (i >= 0 || j >= 0) {let x = 0;let y = 0;let sum = "";if (i >= 0) {x = a[i] - "0";i--;}if (j >= 0) {y = b[j] - "0";j--;}sum = x + y + carry;if (sum >= 10) {carry = 1;sum -= 10;} else {carry = 0;}ret = sum + ret;}if (carry) {ret = carry + ret;}return ret;
};export default add;// add('99999999999999', '1');
// add('1', '99999999999999');
// add('1234', '4321');