admin管理员组文章数量:1026989
var f_drum_min = function myself(a){
alert(a);
$f_min_node.push(a);
for (i=0;i<=$m;i++){
if ($f_leg[i][1]==a){
myself($f_leg[i][0]);
}
}
};
myself($f_leg[i][0]);
breaks the for
loop , how can I make it run multiple times in loop?
var f_drum_min = function myself(a){
alert(a);
$f_min_node.push(a);
for (i=0;i<=$m;i++){
if ($f_leg[i][1]==a){
myself($f_leg[i][0]);
}
}
};
myself($f_leg[i][0]);
breaks the for
loop , how can I make it run multiple times in loop?
-
var f_drum_min = function myself(a)
Not seems legit. – u_mulder Commented May 4, 2015 at 19:14 -
1
@u_mulder: Actually that is valid JavaScript syntax. The identifier
myself
will only exist inside the function, though. – gen_Eric Commented May 4, 2015 at 19:14 - 1 @RocketHazmat thanks, now I know more) – u_mulder Commented May 4, 2015 at 19:15
- 1 What is $f_leg? What is $m? It's hard to get a grasp of where it could be going wrong without a bit more info. – Charlie Wynn Commented May 4, 2015 at 19:15
- 1 What happens when the for loop "breaks"? Is there an error? – nderscore Commented May 4, 2015 at 19:16
2 Answers
Reset to default 4Your function is riddled with bad habits
There's no way for me to improve this function because I have no idea what all of those external states do. Nor is it immediately apparent what their data types are.
These are bad habits because there's no possible way to know the effect of your function. Its only input is a
, yet the function depends on $f_min_node
, $f_leg
, and $m
.
What is the value of those variables at the time you call your function?
What other functions change those values?
I assigned
$f_min_node
to some value and then calledf_drum_min
. How was I supposed to know that$f_min_node
was going to get changed?
Every time you call your function, it's a big surprise what happens as a result. These are the woes of writing non-deterministic ("impure") functions.
Until you can fix these problems, recursion in a for-loop the least of your concerns
I have annotated your code with ments here
// bad function naming. what??
var f_drum_min = function myself(a){
// side effect
alert(a);
// external state: $f_min_node
// mutation: $f_min_node
$f_min_node.push(a);
// leaked global: i
// external state: $m
for (i=0;i<=$m;i++){
// external state: $f_leg
// loose equality operator: ==
if ($f_leg[i][1]==a){
myself($f_leg[i][0]);
}
}
};
I can help you write a deterministic recursive function that uses a linear iterative process though. Most importantly, it doesn't depend on any external state and it never mutates an input.
// Number -> Number
var fibonacci = function(n) {
function iter(i, a, b) {
if (i === 0)
return a;
else
return iter(i-1, b, a+b);
}
return iter(n, 0, 1);
}
fibonacci(6); // 8
for
loops are pretty primitive; Imperative programmers will reach for it almost immediately thinking it's the only way to solve an iteration problem.
I could've used a for
loop in this function, but thinking about the problem in a different way allows me to express it differently and avoid the for
loop altogether.
One basic problem with the code, which would cause it to break under almost any circumstances, is that the loop variable i
is a global, and is thus shared by all recursive invocations of the function.
For example, let's say the function is invoked for the first time. i
is 0. Now it recurses, and let's say that the condition in the if
is never true
. At the end of the 2nd call, i = $m + 1
. When you return to the first call, because i
is global, the loop in the first call ends. I assume this is not what you want.
The fix for this is to declare i
as local:
for (var i=0;i<=$m;i++){
This may or may not fix all of your problems (as pointed out in ments, we'd have to see more of your code to identify all possible issues), but it is a critical first step.
var f_drum_min = function myself(a){
alert(a);
$f_min_node.push(a);
for (i=0;i<=$m;i++){
if ($f_leg[i][1]==a){
myself($f_leg[i][0]);
}
}
};
myself($f_leg[i][0]);
breaks the for
loop , how can I make it run multiple times in loop?
var f_drum_min = function myself(a){
alert(a);
$f_min_node.push(a);
for (i=0;i<=$m;i++){
if ($f_leg[i][1]==a){
myself($f_leg[i][0]);
}
}
};
myself($f_leg[i][0]);
breaks the for
loop , how can I make it run multiple times in loop?
-
var f_drum_min = function myself(a)
Not seems legit. – u_mulder Commented May 4, 2015 at 19:14 -
1
@u_mulder: Actually that is valid JavaScript syntax. The identifier
myself
will only exist inside the function, though. – gen_Eric Commented May 4, 2015 at 19:14 - 1 @RocketHazmat thanks, now I know more) – u_mulder Commented May 4, 2015 at 19:15
- 1 What is $f_leg? What is $m? It's hard to get a grasp of where it could be going wrong without a bit more info. – Charlie Wynn Commented May 4, 2015 at 19:15
- 1 What happens when the for loop "breaks"? Is there an error? – nderscore Commented May 4, 2015 at 19:16
2 Answers
Reset to default 4Your function is riddled with bad habits
There's no way for me to improve this function because I have no idea what all of those external states do. Nor is it immediately apparent what their data types are.
These are bad habits because there's no possible way to know the effect of your function. Its only input is a
, yet the function depends on $f_min_node
, $f_leg
, and $m
.
What is the value of those variables at the time you call your function?
What other functions change those values?
I assigned
$f_min_node
to some value and then calledf_drum_min
. How was I supposed to know that$f_min_node
was going to get changed?
Every time you call your function, it's a big surprise what happens as a result. These are the woes of writing non-deterministic ("impure") functions.
Until you can fix these problems, recursion in a for-loop the least of your concerns
I have annotated your code with ments here
// bad function naming. what??
var f_drum_min = function myself(a){
// side effect
alert(a);
// external state: $f_min_node
// mutation: $f_min_node
$f_min_node.push(a);
// leaked global: i
// external state: $m
for (i=0;i<=$m;i++){
// external state: $f_leg
// loose equality operator: ==
if ($f_leg[i][1]==a){
myself($f_leg[i][0]);
}
}
};
I can help you write a deterministic recursive function that uses a linear iterative process though. Most importantly, it doesn't depend on any external state and it never mutates an input.
// Number -> Number
var fibonacci = function(n) {
function iter(i, a, b) {
if (i === 0)
return a;
else
return iter(i-1, b, a+b);
}
return iter(n, 0, 1);
}
fibonacci(6); // 8
for
loops are pretty primitive; Imperative programmers will reach for it almost immediately thinking it's the only way to solve an iteration problem.
I could've used a for
loop in this function, but thinking about the problem in a different way allows me to express it differently and avoid the for
loop altogether.
One basic problem with the code, which would cause it to break under almost any circumstances, is that the loop variable i
is a global, and is thus shared by all recursive invocations of the function.
For example, let's say the function is invoked for the first time. i
is 0. Now it recurses, and let's say that the condition in the if
is never true
. At the end of the 2nd call, i = $m + 1
. When you return to the first call, because i
is global, the loop in the first call ends. I assume this is not what you want.
The fix for this is to declare i
as local:
for (var i=0;i<=$m;i++){
This may or may not fix all of your problems (as pointed out in ments, we'd have to see more of your code to identify all possible issues), but it is a critical first step.
本文标签: Javascript recursive function inside a for loopStack Overflow
版权声明:本文标题:Javascript recursive function inside a for loop - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660437a2161857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论