![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 139
Rep Power: 4
![]() |
Hello,
My recursive function call doesnt seem to work as the alert statement after it fires code: var imgNumber=1;//this is the array ordinal, start at 1 because arr[0] is displayed already on page load
var numImgs=36;
var conCat;
var images = new Array();
images[0]="18.jpg";
images[1]="xx";
images[2]="19.jpg";
images[3]="28.jpg";
images[4]="xx";
images[5]="xx";
images[6]="1.jpg";
images[7]="21.jpg";
images[8]="xx";
images[9]="xx";
images[10]="20.jpg";
images[11]="24.jpg";
images[12]="30.jpg";
images[13]="27.jpg";
images[14]="35.jpg";
images[15]="9.jpg";
images[16]="12.jpg";
images[17]="15.jpg";
images[18]="2.jpg";
images[19]="10.jpg";
images[20]="13.jpg";
images[21]="14.jpg";
images[22]="36.jpg";
images[23]="25.jpg";
images[24]="22.jpg";
images[25]="xx";
images[26]="29.jpg";
images[27]="31.jpg";
images[28]="32.jpg";
images[29]="39.jpg";
images[30]="38.jpg";
images[31]="26.jpg";
images[32]="37.jpg";
images[33]="3.jpg";
images[34]="8.jpg";
images[35]="xx";
images[36]="16.jpg";
images[37]="17.jpg";
images[38]="5.jpg";
images[39]="34.jpg";
images[40]="6.jpg";
images[41]="11.jpg";
function changePic()
{
if (imgNumber <= images.length-1)
{
if(images[imgNumber]=="xx")
{
alert ("XX found")
imgNumber++;
changePic();
alert ("yy")
}
document.mainPic.src=images[imgNumber];
}
imgNumber++;
}Any ideas? Thnx |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If will fire when the return from the last recursive call is made. Whether or not it fires subsequently will depend upon whether or not any subsequent calls are made. Here's your sequence: You begin with imageNum = 1, image "xx". Because of that, you increment imageNum and make a recursive call. The image will not be "xx", so you don't make another call, but return from the first recursive call. At that point you will get a "yy" alert, then return from the original call. The imgNumber will be 4 and mainPic.src will be 28.jpg.
I might suggest that there is little use for recursion here. Possibly you're just playing around. If so, avail yourself of more alerts in more paths, with more information.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 139
Rep Power: 4
![]() |
Hmm I see. This script is for a website i,m working on. The idea with the recursion is (as you can see) the code will find xx and then increment and look at the next ordinal, but if that next ordinal is also xx it doesnt do anything. The idea is if any ordianl has xx in it it looks at the next one.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
First of all, a loop can do that. Secondly, your explanation still isn't very complete.
You say if will find xx first, then look at the next. If it finds xx it doesn't (does that mean, "isn't supposed to"?) do anything. You don't mention what it should do if it isn't xx. Use it? Take off and find the next xx? What do you want to do, and why is xx involved?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Expert Programmer
|
The changepic functions called all have to return at some point, I don't see any condition that stops them from returning and avoiding the alert function. I think you need a conditional statement in there and need to change the function so that it returns a value and acts accordingly.
__________________
Clifford Matthew Roche <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Avoiding Stackoverflow error using recursion to solve a maze | Mjordan2nd | Java | 1 | Feb 9th, 2006 10:01 PM |
| cgi/perl script + IE problem | joyceshee | Perl | 2 | Jan 24th, 2006 11:10 AM |
| Debug recursion method() | pr0gm3r | Java | 3 | Oct 11th, 2005 12:33 PM |
| Recursion | Mjordan2nd | Coder's Corner Lounge | 22 | Jul 7th, 2005 11:26 AM |
| help with recursion problem | the_new_guy_in_town | Java | 3 | Apr 7th, 2005 3:04 AM |