![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I am a newbie, and after a long day of searching, reading, and trying things, I cannot figure out what I am doing wrong. I know it's something silly, but I could use some help: Here's a simplified version of my code that apparently has the error in it (an explanation of my problem follows): script type="text/javascript" var start_cell_no = horiz_endpoint(4); alert ("result assigned to variable: " + start_cell_no); alert("function result directly: " + horiz_endpoint(4)); function horiz_endpoint(last_cell_chked){ var cell_to_chk = last_cell_chked + 1; if (cell_to_chk == 30){ javascript:alert ("right horizontal end is: " + last_cell_chked); return last_cell_chked; } horiz_endpoint(cell_to_chk); } /script The code snippet correctly iterates until it triggers the function's if statement. It prints the correct value for last_cell_chked. I should then return that same value, but it doesn't. (Or, if it does, the calling statements are somehow bunged up.) Both alert statements in the main script say that the value returned is "undefined. What am I doing that I am not supposed to be doing, or what tittle have I omitted? Thanks very much. |
#3
| |||
| |||
|
|
I am a newbie, and after a long day of searching, reading, and trying things, I cannot figure out what I am doing wrong. I know it's something silly, but I could use some help: Here's a simplified version of my code that apparently has the error in it (an explanation of my problem follows): script type="text/javascript" var start_cell_no = horiz_endpoint(4); alert ("result assigned to variable: " + start_cell_no); alert("function result directly: " + horiz_endpoint(4)); function horiz_endpoint(last_cell_chked){ var cell_to_chk = last_cell_chked + 1; if (cell_to_chk == 30){ javascript:alert ("right horizontal end is: " + last_cell_chked); return last_cell_chked; } horiz_endpoint(cell_to_chk); } /script The code snippet correctly iterates until it triggers the function's if statement. It prints the correct value for last_cell_chked. I should then return that same value, but it doesn't. (Or, if it does, the calling statements are somehow bunged up.) Both alert statements in the main script say that the value returned is "undefined. What am I doing that I am not supposed to be doing, or what tittle have I omitted? |
|
Thanks very much. |
#4
| |||
| |||
|
|
scandal wrote: I am a newbie, and after a long day of searching, reading, and trying things, I cannot figure out what I am doing wrong. I know it's something silly, but I could use some help: Here's a simplified version of my code that apparently has the error in it (an explanation of my problem follows): script type="text/javascript" var start_cell_no = horiz_endpoint(4); alert ("result assigned to variable: " + start_cell_no); alert("function result directly: " + horiz_endpoint(4)); function horiz_endpoint(last_cell_chked){ var cell_to_chk = last_cell_chked + 1; if (cell_to_chk == 30){ javascript:alert ("right horizontal end is: " + last_cell_chked); return last_cell_chked; } horiz_endpoint(cell_to_chk); } /script The code snippet correctly iterates until it triggers the function's if statement. It prints the correct value for last_cell_chked. It should then return that same value, but it doesn't. (Or, if it does, the calling statements are somehow bunged up.) Both alert statements in the main script say that the value returned is "undefined. |
|
But the logic problem is that you are recursing instead of looping, and your recursed function calls are returning to the function that called them, not to the 'main program'. You can either rewrite it as a loop (I really think this is the best solution) or change horiz_endpoint(cell_to_chk); } to return horiz_endpoint(cell_to_chk); } for a quick fix. |
#5
| |||
| |||
|
|
scandal wrote: I am a newbie, and after a long day of searching, reading, and trying things, I cannot figure out what I am doing wrong. I know it's something silly, but I could use some help: Here's a simplified version of my code that apparently has the error in it (an explanation of my problem follows): script type="text/javascript" var start_cell_no = horiz_endpoint(4); alert ("result assigned to variable: " + start_cell_no); alert("function result directly: " + horiz_endpoint(4)); function horiz_endpoint(last_cell_chked){ var cell_to_chk = last_cell_chked + 1; if (cell_to_chk == 30){ javascript:alert ("right horizontal end is: " + last_cell_chked); return last_cell_chked; } horiz_endpoint(cell_to_chk); } /script |
|
You're using recursion when you should be using a conditional loop (for, while, do, etc.). You also make use of the javascript pseudo- protocol in a rather novel manner where it is not required. The function does not appear to do anything useful, I hope the following provides some assistance: function horiz_endpoint( cell_to_chk ){ // While cell_to_check is less than 30, keep adding to it while ( ++cell_to_chk < 30 ) { // do something, or nothing... } alert('right horizontal end is: ' + cell_to_chk ); return cell_to_chk; } |
#6
| ||||
| ||||
|
| RobG wrote: scandal wrote: I am a newbie, and after a long day of searching, reading, and trying things, I cannot figure out what I am doing wrong. I know it's something silly, but I could use some help: Here's a simplified version of my code that apparently has the error in it (an explanation of my problem follows): script type="text/javascript" var start_cell_no = horiz_endpoint(4); alert ("result assigned to variable: " + start_cell_no); alert("function result directly: " + horiz_endpoint(4)); function horiz_endpoint(last_cell_chked){ var cell_to_chk = last_cell_chked + 1; if (cell_to_chk == 30){ javascript:alert ("right horizontal end is: " + last_cell_chked); return last_cell_chked; } horiz_endpoint(cell_to_chk); } /script [snip] You're using recursion when you should be using a conditional loop (for, while, do, etc.). You also make use of the javascript pseudo- protocol in a rather novel manner where it is not required. The function does not appear to do anything useful, I hope the following provides some assistance: function horiz_endpoint( cell_to_chk ){ // While cell_to_check is less than 30, keep adding to it while ( ++cell_to_chk < 30 ) { // do something, or nothing... } alert('right horizontal end is: ' + cell_to_chk ); return cell_to_chk; } Thanks for the salient response. You're right: the code I posted REALLY doesn't do anything useful. I was working my original code over to try to get at the essence of my problem. The real code, given any cell in an array arranged by rows and columns, is supposed to return the ending cell and length of a horizontal line of cells with matching contents. |
|
I chose a recursion over a loop because: 1) I don't know how long the line will be. A loop set up to iterate to row length would work if I return when I reach a non-matching cell; 2) I |
|
was initially lazy and thought recursion was simple. Then, of course, I spent hours and hours trying to figure out why it didn't work. |
|
Do you think a loop would be better, now that I (with help) have the recursion working? All other critique is welcome, though it may be painful. |
#7
| ||||
| ||||
|
|
Christopher J. Hahn wrote: scandal wrote: I am a newbie, and after a long day of searching, reading, and trying things, I cannot figure out what I am doing wrong. I know it's something silly, but I could use some help: Here's a simplified version of my code that apparently has the error in it (an explanation of my problem follows): script type="text/javascript" var start_cell_no = horiz_endpoint(4); alert ("result assigned to variable: " + start_cell_no); alert("function result directly: " + horiz_endpoint(4)); function horiz_endpoint(last_cell_chked){ var cell_to_chk = last_cell_chked + 1; if (cell_to_chk == 30){ javascript:alert ("right horizontal end is: " + last_cell_chked); return last_cell_chked; } horiz_endpoint(cell_to_chk); } /script The code snippet correctly iterates until it triggers the function's if statement. It prints the correct value for last_cell_chked. It should then return that same value, but it doesn't. (Or, if it does, the calling statements are somehow bunged up.) Both alert statements in the main script say that the value returned is "undefined. [snip] [snip] But the logic problem is that you are recursing instead of looping, and your recursed function calls are returning to the function that called them, not to the 'main program'. You can either rewrite it as a loop (I really think this is the best solution) or change horiz_endpoint(cell_to_chk); } to return horiz_endpoint(cell_to_chk); } for a quick fix. Thanks for the quick response. I have tried the last solution - keeping the recursion but having two return statements - and it works. Do I understand this correctly? Let's say it takes 3 iterations to meet the if condition. So, in call 1 the function hits the if statement, doesn't meet the condition, so it "returns" by entering the next iteration. Let me call that call 2. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|
Again, it returns, calling itself - call 3. This time the if conditional is satisfied, the script returns last_cell_chked to call 2, but then what happens? When I thought the original return (conditioned on the if) took it all back to main, it made sense, but - as you have pointed out - that's not the way it actually worked. I don't think this works the way I understand perl working, but maybe I have been operating with a misunderstanding for years and need to try it in perl. |
|
Why would you recommend a loop vs. a recursive solution? I thought loop but cannot tell how many iterations I will need. Can one modify the counter value in the body of a for loop without screwing things up? |
|
Thanks again. |
![]() |
| Thread Tools | |
| Display Modes | |
| |