To spilt a number into digits and print their sum, try to run the following code −
Example
<!DOCTYPE html>
<html>
<body>
<script>
var a = 28573;
var sum = 0;
while(a > 0) {
sum += a % 10;
a = Math.floor(a / 10);
}
document.write("Sum of digits = "+sum);
</script>
</body>
</html>Output
Sum of digits = 25