@anandabastakoti/sum-of-digits
Version:
This is the firs npm package I developed in which the function takes an positive integer and returns the sum of all the digits
15 lines (12 loc) • 418 B
JavaScript
function sumOfDigits(input) {
if (typeof input === "string") return "Only number inputs are allowed!";
if (typeof input === "boolean") return "Only number inputs are allowed!";
if (typeof input !== "number" || input < 0)
return "Input must be a positive number!";
return input
.toString()
.split("")
.map(Number)
.reduce((a, b) => a + b, 0);
}
module.exports = sumOfDigits;