Calculate Sum of Digits in a Number

This snippet defines a function `findSumOfDigits` that calculates the sum of the digits of a given number. It uses a while loop to extract each digit by taking the modulus and then reduces the number by dividing it. Finally, it logs the sum of the digits of the number 77.
mail@pastecode.io avatar
unknown
javascript
17 days ago
240 B
4
Indexable
Never
const findSumOfDigits = (num) => {
    let sum = 0;

    while (num != 0)
    {
        let digit = num % 10;
        sum += digit;
        num = Math.floor(num / 10);
    }

    return sum;
}

console.log(findSumOfDigits(77));
Leave a Comment