# numAdd

let a = "9007199254740991";
let b = "1234567899999999999";

function add(a, b) {
  let maxLength = Math.max(a.length, b.length);

  a = a.padStart(maxLength, 0);
  b = b.padStart(maxLength, 0);

  let t = 0;
  let f = 0;
  let sum = "";
  for (let i = maxLength - 1; i >= 0; i--) {
    t = parseFloat(a[i]) + parseFloat(b[i]) + f;
    f = Math.floor(t / 10);

    sum = (t % 10) + sum;
  }

  if (f > 1) {
    sum = "1" + sum;
  }

  return sum;
}

console.log(add(a, b));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Last Updated: 6/27/2023, 7:40:45 PM