Skip to content

Instantly share code, notes, and snippets.

@santosh
Created December 27, 2016 10:02
Show Gist options
  • Save santosh/c9710c67fac79a0cd9c3c44641d15a13 to your computer and use it in GitHub Desktop.
Save santosh/c9710c67fac79a0cd9c3c44641d15a13 to your computer and use it in GitHub Desktop.
Calculate sum of digits in a number. e,g, 1234 = 10. #JavaScript
function digitSum(n) {
n = parseInt(n).toString()
var sum = 0;
for (var i = 0; i < n.length; i++) {
sum += parseInt(n[i]);
}
return sum;
}
@santosh
Copy link
Author

santosh commented Dec 27, 2016

This function has a bug. But works most of the time. Find the bug! 😝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment