Implement the addition of two large integers outside the integer range
Implement a function that adds two large integers beyond the storage range of js integers. The arguments and return values are strings. For example, input: ‘1111111111’, ‘2222222222’ Output: ‘3333333333’
Function add(str1, str2) {var arr1 = str1.split(''), var arr2 = str2.split(''), res = ''; while(arr1.length || arr2.length || extra) { sum = parseInt(arr1.pop()) + parseInt(arr2.pop()) + extra; //parseInt() converts a character to a numeric value for computation. res = sum % 10 + res; extra = sum > 10; } return res } var sum = add('1111111111111','2222222222'); console.log(sum);Copy the code