import addDigits from './addDigits';
import getDigits from './getDigits';
+import joinDigits from './joinDigits';
/**
* Adds two numbers `a` and `b`, both in `base` and returns the answer as a
export default function add(a: string, b: string, base: number): string {
const aDigits = getDigits(a, base);
const bDigits = getDigits(b, base);
- return addDigits(aDigits, bDigits, base)
- .reverse()
- .map(number => number.toString(base))
- .join('');
+ const result = addDigits(aDigits, bDigits, base);
+ return joinDigits(result, base);
}
--- /dev/null
+/**
+ * Copyright 2015-present Greg Hurrell. All rights reserved.
+ * Licensed under the terms of the MIT license.
+ *
+ * @flow
+ */
+
+'use strict';
+
+/**
+ * Turns an unpacked arbitrary-precision representation of a number, `digits`,
+ * (as produced by `getDigits`) back into a string representation in `base`.
+ */
+export default function joinDigits(
+ digits: Array<number>,
+ base: number
+): string {
+ return digits.reverse()
+ .map(number => number.toString(base))
+ .join('');
+}