Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

javascript - 汇总一个数字,直到变成1位数JS(Sum up a number until it becomes 1 digit JS)

Before you mark duplicate note that it isn't.(在标记重复注释之前,请注意不是。)

Others didn't look for the exact same thing as me.(其他人没有寻找和我完全一样的东西。)

What is the most compact possible way to sum up a number in javascript until there is only one digit left.(总结javascript中直到最剩下一位数字的最紧凑的方法是什么。)

For example: You input 5678 then the script adds it together (5+6+7+8) and gets 26, but since its more than 1 digit it adds it again and gets 2+6=8.(例如:您输入5678,然后脚本将其加在一起(5 + 6 + 7 + 8)并得到26,但是由于其大于1的数字,它将再次添加它并得到2 + 6 = 8。)

Is there anyway to do this with a number of any size?(无论如何,有任何大小的数字都可以做到这一点吗?)

And how compact can the script get?(脚本可以得到多紧凑?)   ask by Vincent translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you're looking for short, it's hard to beat:(如果您正在寻找短片,那就很难被击败:)

 var n = 5678; sum = n % 9 || 9; console.log(sum) 

If you're curious about how that works, see: casting out nines.(如果您对它的工作方式感到好奇,请参阅: 淘汰9。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...