Javascript中的乘法持久性程序不工作
我的程序无法运行。问题是Codewars的kata: 编写一个名为persistence的函数,它接受一个正参数num并返回其乘法持久性,即必须将num中的数字相乘直到达到一个位数的次数。 例如:Javascript中的乘法持久性程序不工作,javascript,Javascript,我的程序无法运行。问题是Codewars的kata: 编写一个名为persistence的函数,它接受一个正参数num并返回其乘法持久性,即必须将num中的数字相乘直到达到一个位数的次数。 例如: persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4 // and 4 has only one digit persistence(999) === 4 // because 9*9*9 = 72
persistence(39) === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
// and 4 has only one digit
persistence(999) === 4 // because 9*9*9 = 729, 7*2*9 = 126,
// 1*2*6 = 12, and finally 1*2 = 2
persistence(4) === 0 // because 4 is already a one-digit number
我已经在这里找到了类似问题的答案。这是我的代码:
var count = 0;
var product = 1;
function persistence(num) {
if (num.toString().length == 1) {
count+=0;
return count;
}
for (i of num.toString()) {
product *= Number(i);
}
count++;
var newProduct = product;
// reset product to 1 so that line ten does not
// start with the product from the last loop
product = 1;
persistence(newProduct);
}
我不知道问题出在哪里。最初,我得到了一个超过最大调用堆栈的错误。我对此进行了研究,并意识到我这样做是为了我的基本情况:
if (num.length == 1) {
count+=0;
return count;
}
而不是
if (num.toString().length == 1) {
count+=0;
return count;
}
我的逻辑似乎是正确的。有什么问题吗?用“of”代替“in”。“in”查找属性。“of”增加一个数组
var count = 0;
var product = 1;
function persistence(num) {
if (num.toString().length == 1) {
count+=0;
return count;
}
for (i of num.toString()) {
product *= Number(i);
}
count++;
var newProduct = product;
// reset product to 1 so that line ten does not
// start with the product from the last loop
product = 1;
persistence(newProduct);
}
用“of”代替“in”。“in”查找属性。“of”增加一个数组
var count = 0;
var product = 1;
function persistence(num) {
if (num.toString().length == 1) {
count+=0;
return count;
}
for (i of num.toString()) {
product *= Number(i);
}
count++;
var newProduct = product;
// reset product to 1 so that line ten does not
// start with the product from the last loop
product = 1;
persistence(newProduct);
}
我敢肯定是这个街区:
for (i in num.toString()) {
product *= Number(i);
}
这是一个for…in循环,用于迭代对象中的键。如果要将num
字符串的每个数字相乘,可以将字符串拆分为一个数组,并使用reduce方法():
一般来说,避免在函数范围外声明全局变量是最佳做法,但您可以使用递归执行一个很酷的技巧,将计数声明为函数中的参数,如下所示:
function persistence(num, count = 0) {
然后,当您再次使用递归调用它时,只需添加1,如下所示:
function persistence(product, count + 1) {
我敢肯定是这个街区:
for (i in num.toString()) {
product *= Number(i);
}
这是一个for…in循环,用于迭代对象中的键。如果要将num
字符串的每个数字相乘,可以将字符串拆分为一个数组,并使用reduce方法():
一般来说,避免在函数范围外声明全局变量是最佳做法,但您可以使用递归执行一个很酷的技巧,将计数声明为函数中的参数,如下所示:
function persistence(num, count = 0) {
然后,当您再次使用递归调用它时,只需添加1,如下所示:
function persistence(product, count + 1) {
这里有一个更好的方法来解决你的问题,包括一些我认为可以非常清楚地解释发生了什么的评论
function persistence(num) {
// Create a new function that you'll use inside your main function
function multiply(n) {
// Multiply the first number by next number in the array
// until the entire array has been iterated over
return n.reduce(function(a,b){return a*b;});
}
// Set the count to 0
var count =0;
// Use a while loop to iterate the same number of times
// as there are digits in num
while (num.toString().length > 1) {
// Splits the num into an array
num= num.toString().split("");
// Runs multiply on our num array and sets num to the returned
// value in preparation of the next loop.
// The multiply function will for 39 run 3 * 9 = 27,
// next iteration we've set num to 27 so multiply will be
// 2 * 7 = 14, then 1 * 4 = 4, and since num 4 now
// has a length <= 1 the loop stops.
num = multiply(num);
// Increase count by 1 each iteration, then run the next
// iteration in the loop with the new value for num being
// set to the result of the first multiplication.
count++;
}
return count; // return the value of count
}
console.log(persistence(39));// === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
// and 4 has only one digit
console.log(persistence(999));// === 4 // because 9*9*9 = 729, 7*2*9 = 126,
// 1*2*6 = 12, and finally 1*2 = 2
console.log(persistence(4));// === 0 // because 4 is already a one-digit number
函数持久性(num){
//创建一个将在主函数中使用的新函数
函数乘法(n){
//将第一个数字乘以数组中的下一个数字
//直到对整个数组进行迭代
返回n.reduce(函数(a,b){returna*b;});
}
//将计数设置为0
var计数=0;
//使用while循环重复相同的次数
//因为num中有数字
while(num.toString().length>1){
//将num拆分为一个数组
num=num.toString().split(“”);
//在num数组上运行乘法并将num设置为返回的值
//值,以准备下一个循环。
//对于39,乘法函数将运行3*9=27,
//下一次迭代我们将num设置为27,所以将使用乘法
//2*7=14,然后1*4=4,从现在的num 4开始
//有一个长度这里有一个更好的方法来解决你的问题,包括一些我认为可以非常清楚地解释发生了什么的评论
function persistence(num) {
// Create a new function that you'll use inside your main function
function multiply(n) {
// Multiply the first number by next number in the array
// until the entire array has been iterated over
return n.reduce(function(a,b){return a*b;});
}
// Set the count to 0
var count =0;
// Use a while loop to iterate the same number of times
// as there are digits in num
while (num.toString().length > 1) {
// Splits the num into an array
num= num.toString().split("");
// Runs multiply on our num array and sets num to the returned
// value in preparation of the next loop.
// The multiply function will for 39 run 3 * 9 = 27,
// next iteration we've set num to 27 so multiply will be
// 2 * 7 = 14, then 1 * 4 = 4, and since num 4 now
// has a length <= 1 the loop stops.
num = multiply(num);
// Increase count by 1 each iteration, then run the next
// iteration in the loop with the new value for num being
// set to the result of the first multiplication.
count++;
}
return count; // return the value of count
}
console.log(persistence(39));// === 3 // because 3*9 = 27, 2*7 = 14, 1*4=4
// and 4 has only one digit
console.log(persistence(999));// === 4 // because 9*9*9 = 729, 7*2*9 = 126,
// 1*2*6 = 12, and finally 1*2 = 2
console.log(persistence(4));// === 0 // because 4 is already a one-digit number
函数持久性(num){
//创建一个将在主函数中使用的新函数
函数乘法(n){
//将第一个数字乘以数组中的下一个数字
//直到对整个数组进行迭代
返回n.reduce(函数(a,b){returna*b;});
}
//将计数设置为0
var计数=0;
//使用while循环重复相同的次数
//因为num中有数字
while(num.toString().length>1){
//将num拆分为一个数组
num=num.toString().split(“”);
//在num数组上运行乘法并将num设置为返回的值
//值,以准备下一个循环。
//对于39,乘法函数将运行3*9=27,
//下一次迭代我们将num设置为27,所以将使用乘法
//2*7=14,然后1*4=4,从现在的num 4开始
//有一个长度你的persistence
函数实际上没有返回语句,所以它返回未定义的。你永远不应该在函数内部运行函数。这是一个非常糟糕的做法。我正在为你编写一个漂亮的小JSFIDLE,等一会儿,我应该为你完成它…@user184994这部分代码应该是返回值的位置:如果(num.toString().length==1){count+=0;return count;}@SimonHyll递归呢?如果有正确的分支,它会work@Wryhder,对不起,我的意思是在使用递归的地方,所以还需要返回持久性(newProduct);
你的持久性
函数实际上没有返回语句,因此它返回未定义的。你永远不应该在函数内部运行函数。这是一个非常糟糕的做法。我正在为你编写一个漂亮的小JSFIDLE,等一会儿,我应该为你完成它…@user184994这部分代码是支持的osed是返回值的位置:如果(num.toString().length==1){count+=0;return count;}@SimonHyll递归呢?如果有正确的分支,它会work@Wryhder,对不起,我的意思是在使用递归的地方,所以还需要返回持久性(newProduct);
我已经做了更改。谢谢Number。toString
返回的是字符串,而不是数组。在Javascript中,无法通过字符串进行迭代。*但是可以使用String.split(“”)将字符串转换为字符串array@SimonHyll,在这里我要说的是,str.split(“”)
从字符串值str
返回一个新数组,它不会将str
转换为数组;)我已经做了更改。谢谢Number。toString
返回一个字符串
而不是数组。在Javascript中,无法通过字符串进行迭代。*您可以