代码是这样的:)
function test3(num) {
if (num <= 9) {
return num;
}
let rest = 1;
while (num) {
rest = rest * (num % 10);
num = parseInt(num / 10);
}
if (rest <= 9) {
return rest;
}
return test3(rest);
}
debugger;
let output = test3(786);
console.log(output); // --> 0
我理解其他逻辑,但我不理解'parseInt'如何工作parseInt逻辑?显然我已经读过mdn了,任何stackover flow的答案,都不能很好地理解…
这里使用parseInt()
将小数点后可能有分数的数字转换为整数部分。
它可以工作,因为如果parseInt()
的参数不是字符串,它首先被转换为字符串,然后从中解析一个整数前缀。因此,parseInt(3.14)
等于parseInt('3.14')
,返回3
。
在这里相当于Math.floor()
。