Created
December 27, 2016 10:02
-
-
Save santosh/c9710c67fac79a0cd9c3c44641d15a13 to your computer and use it in GitHub Desktop.
Calculate sum of digits in a number. e,g, 1234 = 10. #JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function digitSum(n) { | |
n = parseInt(n).toString() | |
var sum = 0; | |
for (var i = 0; i < n.length; i++) { | |
sum += parseInt(n[i]); | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This function has a bug. But works most of the time. Find the bug! 😝