Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Recursion problem (http://www.programmingforums.org/showthread.php?t=13119)

paulchwd May 6th, 2007 1:40 PM

Recursion problem
 
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

DaWei May 6th, 2007 2:41 PM

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.

paulchwd May 6th, 2007 4:03 PM

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.

DaWei May 6th, 2007 6:47 PM

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?

kurifu May 7th, 2007 1:47 PM

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.


All times are GMT -5. The time now is 10:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC