检查给定的数字是否是水仙花数。
将给定的数字转换为数字数组。 使用指数运算符(**
)为每个数字获得适当的 N 次方并将其相加。 如果总和等于数字本身,则返回 true
,否则返回 false
。
const isArmstrongNumber = digits => (arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)( (digits + '').split('') );
isArmstrongNumber(1634); // true isArmstrongNumber(371); // true isArmstrongNumber(56); // false
注:在数论中,水仙花数(Narcissistic number),也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number) ,用来描述一个N位非负整数,其各位数字的N次方和等于该数本身。
更多代码 JavaScript 实用代码片段 请查看 https://www.7psus5.com/30-seconds-of-code/
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂