-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sum of Digits / Digital Root #4
Comments
function digital_root(n) {
return String(n).split('').map(Number).reduce((i,j) => i+j)
} |
不知道为啥这个答案过不了 |
哎呀,被你给的信息给误导了。看了原网站的题目才发现,是这个意思: 942 是这个意思 |
来个递归就好了 function digital_root(n) {
var fn = function() {
return String(n).split('').map(Number).reduce((i,j) => i+j)
}
return fn() > 9 ? digital_root(fn()) : fn()
} |
|
哈哈,我粗心了。好像代码格式还是没有你的好看 |
这个题目排名最高的答案给我的感觉就是——程序的数学之美。 |
能否解惑下这个答案呢,😂 |
хехе |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this kata, you must create a digital root function.
A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has two digits, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.
Here's how it works (Ruby example given):
digital_root(942)
=> 9 + 4 + 2
=> 15 ...
=> 1 + 5
=> 6
digital_root(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ...
=> 2 + 4
=> 6
digital_root(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ...
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2
Your Test Cases:
Test.assertEquals( digital_root(16), 7 )
where By
http://www.codewars.com/kata/541c8630095125aba6000c00/train/javascript
The text was updated successfully, but these errors were encountered: