英文:
The count isn't increased
问题
为什么'i'没有增加?问题出在哪里?它在控制台输出'0'
function digitalRoot(n) {
i = 0;
arr = [];
let length = n.length;
while (i < length) {
arr.push(n[i]);
i++;
}
console.log(i); // 输出: 0
}
digitalRoot(11);
英文:
Why is 'i' not increased? What's the problem? It outputs '0' in console
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function digitalRoot(n) {
i = 0;
arr = [];
let length = n.length;
while (i < length) {
arr.push(n[i]);
i++;
}
console.log(i); // output: 0
}
digitalRoot(11);
<!-- end snippet -->
答案1
得分: 2
这是一个数字根计算。
它将循环执行,直到输入的数字的各位数字之和变为单个数字为止。
数字根(也称为重复数字和)是在给定基数下自然数的一个值,通过迭代的方式计算各位数字的和来获得(单个数字)值,每次迭代都使用前一次迭代的结果来计算各位数字之和。这个过程一直持续,直到达到一个单个数字。例如,在十进制中,数字12345的数字根是6,因为数字中各位数字的和为1 + 2 + 3 + 4 + 5 = 15,然后对结果数字15再次执行加法过程,所以1 + 5的和等于6,这就是该数字的数字根。维基百科
const digitalRoot = num => {
let len = num.toString().length;
while (len > 1) {
num = [...num.toString()].reduce((a, b) => (+a) + (+b));
len = num.toString().length;
}
return num;
};
console.log(digitalRoot(11)); // 2
console.log(digitalRoot(1999)); // 1
在十进制中,这等同于除以9取余数(除非数字根为9,在这种情况下,除以9的余数将为0),这使它可以用作可除性规则。
const digitalRoot = num => {
if (num === 0) return 0;
if (num % 9 === 0) return 9;
return num % 9;
};
console.log(digitalRoot(11)); // 2
console.log(digitalRoot(1999)); // 1
如果你想要乘法数字根,你可以这样做:
const multiplicativeDigitalRoot = num => {
let persistence = 0;
while (num > 9) {
num = [...num.toString()].reduce((a, b) => a * b);
console.log(num);
persistence++;
}
return { root: num, persistence }; // 第二个数字是迭代次数
};
console.log(multiplicativeDigitalRoot(11)); // 1,1
console.log(multiplicativeDigitalRoot(1999)); // 2,4
console.log(multiplicativeDigitalRoot(277777788888899)); // 0, 11
英文:
Here is a digitalRoot calculation
It will loop until the sum of the digits in the input has the length 1
>The digital root (also repeated digital sum) of a natural number in a given radix is the (single digit) value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached. For example, in base 10, the digital root of the number 12345 is 6 because the sum of the digits in the number is 1 + 2 + 3 + 4 + 5 = 15, then the addition process is repeated again for the resulting number 15, so that the sum of 1 + 5 equals 6, which is the digital root of that number. <sub><a href="https://en.wikipedia.org/wiki/Digital_root">Wikipedia</a><sub>
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const digitalRoot = num => {
let len = num.toString().length;
while (len > 1) {
num = [...num.toString()].reduce((a,b) => (+a) + (+b));
len = num.toString().length;
}
return num
};
console.log(digitalRoot(11)); // 2
console.log(digitalRoot(1999)); // 1
<!-- end snippet -->
>In base 10, this is equivalent to taking the remainder upon division by 9 (except when the digital root is 9, where the remainder upon division by 9 will be 0), which allows it to be used as a divisibility rule.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const digitalRoot = num => {
if (num === 0) return 0;
if (num % 9 === 0) return 9;
return num % 9;
};
console.log(digitalRoot(11)); // 2
console.log(digitalRoot(1999)); // 1
<!-- end snippet -->
If you want the multiplicative digitalRoot you can do this
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const multiplicativeDigitalRoot = num => {
let persistence = 0;
while (num > 9) {
num = [...num.toString()].reduce((a, b) => a * b);
console.log(num)
persistence++;
}
return { root: num, persistence }; // second number is how many iterations it took
};
console.log(multiplicativeDigitalRoot(11)); // 1,1
console.log(multiplicativeDigitalRoot(1999)); // 2,4
console.log(multiplicativeDigitalRoot(277777788888899)); // 0, 11
<!-- end snippet -->
答案2
得分: 1
以下是翻译好的内容:
我猜你想要一个数字数组:
// 开始代码片段:js 隐藏:false 控制台:true Babel:false
// 语言:lang-js
function digitalRoot(n) {
const str = n.toString()
i = 0;
arr = [];
while (i < str.length) {
arr.push(str[i]);
i++;
}
console.log(i);
console.log(arr)
}
digitalRoot(11);
// 结束代码片段
更短的方式:
// 开始代码片段:js 隐藏:false 控制台:true Babel:false
// 语言:lang-js
function digitalRoot(n) {
console.log([...n.toString()])
}
digitalRoot(11);
// 结束代码片段
英文:
I guess that you want to have an array of digits:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function digitalRoot(n) {
const str = n.toString()
i = 0;
arr = [];
while (i < str.length) {
arr.push(str[i]);
i++;
}
console.log(i);
console.log(arr)
}
digitalRoot(11);
<!-- end snippet -->
Shorter:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function digitalRoot(n) {
console.log([...n.toString()])
}
digitalRoot(11);
<!-- end snippet -->
答案3
得分: -2
因为你在 "digitalRoot(11);" 中传递了一个数字,所以 "n.length" 将为未定义,而 While 永远不会被触发。
如果你传递一个数组,它将正常工作!
英文:
Since you're passing a number in "digitalRoot(11);", the "n.length" will be undefined and the While is never triggered.
If you pass down an array instead, it will work just fine!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论