2 回答
TA贡献1799条经验 获得超6个赞
您可能想reduce再次拆分结果:
let persistence = (num) => {
let tempoOne, tempoTwo;
let count = 0;
let numArray = num.toString().split('');
var calculator = (accumulator, currentValue) => accumulator * currentValue;
var result;
tempoOne = numArray;
do {
result = tempoOne.reduce(calculator);
tempoOne = result.toString().split('');
count++;
} while (result / 10 > 1);
return count;
}
console.log(persistence(939));
无result变量:
let persistence = (num) => {
let tempoOne, tempoTwo;
let count = 0;
let numArray = num.toString().split('');
var calculator = (accumulator, currentValue) => accumulator * currentValue;
var result;
tempoOne = numArray;
do {
tempoOne = tempoOne.reduce(calculator).toString().split('');
count++;
} while (tempoOne.length > 1);
return count;
}
console.log(persistence(939));
TA贡献1921条经验 获得超9个赞
把你tempoOne回string并split过来迭代:
let persistence = (num) => {
let tempoOne, tempoTwo;
let count = 0
var calculator = (accumulator, currentValue) => accumulator*parseInt(currentValue);
tempoOne = num;
do {
tempoOne = tempoOne.toString().split('').reduce(calculator, 1);
count++;
} while (tempoOne/10 > 1);
return count;
}
console.log(persistence(939));
我注意到您的方法中的一些错误可能会导致一些错误:
当你
split()
是一个字符串时,你会得到一个字符串数组,所以你必须在乘以它之前将它解析为 int ,因为你不想乘以字符串(添加parseInt
到你的reduce callback
并以中性乘法因子开始:)1
。您不必
split
跳出循环,因为您想tempoOne
成为循环内的字符串才能split()
正常工作(删除了split
外部循环并添加到循环中)。
添加回答
举报