Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 6th, 2007, 1:40 PM   #1
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
Question 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
paulchwd is offline   Reply With Quote
Old May 6th, 2007, 2:41 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 6th, 2007, 4:03 PM   #3
paulchwd
Hobbyist Programmer
 
paulchwd's Avatar
 
Join Date: Mar 2005
Posts: 139
Rep Power: 4 paulchwd is on a distinguished road
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.
paulchwd is offline   Reply With Quote
Old May 6th, 2007, 6:47 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old May 7th, 2007, 1:47 PM   #5
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
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 &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:24 AM.

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