虽然这个问题here的答案是递归方面的最佳答案,但
function sumDigits(number) {
var remainder = number % 10;
var sum = remainder;
if(number >= 10) {
var rest = Math.floor(number / 10);
sum += sumDigits(rest);
}
return sum;
}
还有另一个答案似乎更容易理解。但是,我不明白答案的某些部分。
function sumDigits(number) {
var sum = 0;
var numbers = number.toString().split(""); //turn the number to string and split the number in several digits
while(numbers.length > 0) {
sum += parseInt(numbers[0], 10); // covert string to number and force the number to be decimal and add the next number
numbers.splice(0, 1); //remove the first digit, in other words, removing "0" from sum, i think?
sumDigits(numbers.join('')); //join the array element into a string
}
return sum;
}
var sum = sumDigits(175);
$('#result').html(sum);
这是我能够理解的:
var numbers = number.toString().split("");
将数字转换为字符串并将字符串拆分为子字符串数组。换句话说,如果数字是175,它将被转换为“175”并分成1,7,5。接下来,
while(numbers.length > 0) {
这可以确保数字是正数并且可以添加。但是,当我删除此行并在JS Bin中以负数运行它时,它会返回错误。所以,不知何故,仅仅总结正数就很重要。接着,
sum += parseInt(numbers[0], 10);
这个(parseInt)将字符串或我们的情况下的子字符串转换回数字并将每个数字添加到自身。但是我不理解数字后括号中的“0”,除非它指的是数组中的第一个数字= [1,7,5]?不过,我可以看到,当我从这一行中删除[0]时,它会返回一个错误。另外,使用“sum + =”,它应该像这样加0 + 1 + 7 + 5.因此总和是13.接下来,
numbers.splice(0, 1);
这是我不理解的部分。我知道.splice要么添加要么删除项目。例如,(0,1)的第一部分,即“0”,找到数组的位置,换句话说,“0”找到数组中“1”之前的第一个位置= [1,7 ,5]。最后一部分是“1”,指定要删除的项目数量。换句话说,它要求从数组中删除“1”,导致数组现在为[7,5]。但是,我认为根本不需要它,因为不需要从数字中删除任何数字。另外,我不明白这句话说当这笔总和应该是13时从该总和中删除“0”的评论?接着,
sumDigits(numbers.join(''));
这应该将数组元素连接成一个字符串。换句话说,它应该将array = [1,7,5]加入“1,7,5”。不过,我不明白为什么当这一行(sum + = parseInt(numbers [0]))将字符串转换回数字并将数字加在一起时,为什么需要这两行(.splice和.join)。但是,当我删除这两行时,该功能不起作用......
请帮助我理解它是如何工作的,哪些是先执行等等......
答案 0 :(得分:1)
你几乎是对的。
使用的基本算法是
将数字转换为字符串number.toString()
并将此字符串拆分为字符数组number.toString().split("")
,每个字符代表原始数字的一位数
var numbers = number.toString().split("");
虽然结果数组包含多于零个元素
while(numbers.length > 0) {
将数组numbers[0]
的第一个元素转换为数字parseInt(numbers[0], 10)
并将其添加到sum
sum += parseInt(numbers[0], 10);
从numbers
numbers.splice(0, 1);
返回2.如果剩下任何元素,则使用较短的数组numbers
重复。请注意,先前的第二个元素现在是第一个元素
numbers
元素为零后,返回sum
此步骤似乎完全没用,应删除:
sumDigits(numbers.join(''));
答案 1 :(得分:1)
我们第一次运行该函数时会将其拆分:
numbers = ['3','4','3','2']
然后在这个循环中,我们将完成该过程中最偏向的部分,让我们看看:
while(numbers.length > 0) { //actually the condition here is number array has to have at least 1 element, we'll see why
sum += parseInt(numbers[0], 10); // covert string to number and force the number to be decimal and add the next number
//so sum = 0+3
numbers.splice(0, 1); //remove the first digit, in other words, removing "0" from sum, i think?
//then here you're right, we remove the first digit so that
number = ['4','3','2']
sumDigits(numbers.join('')); //join the array element into a string
//and here we call recursively sumDigits passing "432" in arguments
/* so next time sum = 3+4, number = ['3','2'] and then
we will call sumDigits again with "32" as arguments
and then sum = 7+3, number = ['2'], and then
we will call sumDigits again with "2" as arguments
and then sum = 10+2 and number = [] and then we will call sumDigits again passing it "" and at that time the while loop break */
}
答案 2 :(得分:0)
function addDigits(n) {
let str = n.toString().split('');
let len = str.length;
let add,
acc = 0;
for (i=0; i<=len-1; i++) {
acc += Number(str[i]);
}
return acc;
}
console.log( addDigits(123456789) ); //Output: 45