Created
September 4, 2016 14:35
-
-
Save joshkautz/c88031bd46681f77b9cb3e51d25a2d9c to your computer and use it in GitHub Desktop.
CodeFights - JavaScript code to calculate the sum of all digits in an integer
This file contains 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) { | |
var sum = 0; | |
var string = n.toString(); | |
for(i=0; i < string.length; i++){ | |
sum = sum + parseInt(string.substring(i, i+1)); | |
} | |
return sum; | |
} |
You are right. Sorry about that. The n%9 is only if you want to keep adding the digits until you have only one digit left.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sorry but, if your n = 28, the output will be 1 ( 28%9 = 1 ) instead of 10