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 for (let i = 0; i < aDigits.length || i < bDigits.length || carry; i++) {
16 const aDigit = i < aDigits.length ? aDigits[i] : 0;
17 const bDigit = i < bDigits.length ? bDigits[i] : 0;
18 const sum = aDigit + bDigit + carry;
19 result.push(sum % base);
21 // ~~ here is the equivalent of Math.floor; used to avoid V8 de-opt,
22 // "Reference to a variable which requires dynamic lookup".
23 carry = ~~(sum / base);
25 return result.length ? result : [0];