everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
21 lines (20 loc) • 535 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.digitSumInBase = void 0;
/**
* Sums the digits of a number in an arbitrary base.
* @author @dailker
* @param {number} n - The number.
* @param {number} base - The base.
* @returns {number} Sum of digits.
*/
function digitSumInBase(n, base) {
let sum = 0;
n = Math.abs(n);
while (n > 0) {
sum += n % base;
n = Math.floor(n / base);
}
return sum;
}
exports.digitSumInBase = digitSumInBase;