2 * Copyright 2003-present Greg Hurrell. All rights reserved.
3 * Licensed under the terms of the MIT license.
8 export default function addDigits(
9 aDigits: Array<number>,
10 bDigits: Array<number>,
15 const aLength = aDigits.length;
16 const bLength = bDigits.length;
17 for (let i = 0; i < aLength || i < bLength || carry; i++) {
18 const aDigit = i < aLength ? aDigits[aLength - i - 1] : 0;
19 const bDigit = i < bLength ? bDigits[bLength - i - 1] : 0;
20 const sum = aDigit + bDigit + carry;
21 result.unshift(sum % base);
23 // ~~ here is the equivalent of Math.floor; used to avoid V8 de-opt,
24 // "Reference to a variable which requires dynamic lookup".
25 carry = ~~(sum / base);
27 return result.length ? result : [0];