Last active
June 5, 2017 02:01
-
-
Save alejandrolechuga/0dcbbd8a1561d5e8df4f6b33c2c4a3bb to your computer and use it in GitHub Desktop.
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
// digit sum | |
// easy way | |
function digitSum(number) { | |
number = Array | |
.from(number.toString()) | |
.reduce(function(acc, n){ | |
return acc + parseInt(n); | |
},0); | |
if (number > 9) number = digitSum(number); | |
return number; | |
} | |
console.log(digitSum(3219)); |
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 addToDigit(number) { | |
var acc = 0; | |
while(number > 0) { | |
var reminder = number % 10; | |
number = (number - reminder) / 10; | |
acc += reminder; | |
} | |
if (acc > 9) acc = addToDigit(acc); | |
return acc; | |
} | |
console.log(addToDigit(3251)); // => 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment