乘以数字的每个元素,直到长度为1
如何将数字的每个元素乘以数字只有一个数字?
function persistence(number) {
let a = number.toString();
let b = 1;
for (const ch of a) {
b *= +ch
}
console.log(b) // 27
}
// persistence(39)
// 39 --> 4 (because 3*9 = 27, 2*7 = 14, 1*4 = 4 and 4 has only one digit)
How to multiply every element of a number until number has one digit only?
function persistence(number) {
let a = number.toString();
let b = 1;
for (const ch of a) {
b *= +ch
}
console.log(b) // 27
}
// persistence(39)
// 39 --> 4 (because 3*9 = 27, 2*7 = 14, 1*4 = 4 and 4 has only one digit)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用递归调用
persistence()
函数,直到结果具有length == 1
You could call the
persistence()
function with recursion until the result haslength == 1