mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-19 01:55:51 +08:00
17 lines
331 B
JavaScript
17 lines
331 B
JavaScript
// program to find sum of digits of a number
|
|
|
|
// function which would calculate sum and return it
|
|
const digitSum = (num) => {
|
|
// sum will store sum of digits of a number
|
|
let sum = 0
|
|
// while will run until num become 0
|
|
while (num) {
|
|
sum += num % 10
|
|
num = parseInt(num / 10)
|
|
}
|
|
|
|
return sum
|
|
}
|
|
|
|
export { digitSum }
|