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.
18 lines (17 loc) • 531 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.digitalRoot = void 0;
/**
* Calculates the digital root of a number (sum of digits until a single digit).
* For example, digitalRoot(942) returns 6.
* @author @dailker
* @param {number} n - The number to process.
* @returns {number} The digital root.
*/
function digitalRoot(n) {
while (n >= 10) {
n = n.toString().split('').reduce((a, b) => a + +b, 0);
}
return n;
}
exports.digitalRoot = digitalRoot;