How to sum the digits of a number in TypeScript

1 Answer

0 votes
function sumDigits(num : number) : number {
    let sum = 0;
    while (num != 0) {
        sum += num % 10;
        num = Math.floor(num / 10);
    }
    return sum;
}
  
 
const n = 82061;
      
console.log(sumDigits(n));
 
   
     
     
/*
run:
     
17 
     
*/

 



answered Jun 20, 2022 by avibootz
...