开发者

how to stop recursion and/or restart the current function with updated variables

开发者 https://www.devze.com 2023-01-31 08:01 出处:网络
I\'ve got two arrays which are updating themselves and each other based on criteria (it is way longer to describe than I suspect the solution is).

I've got two arrays which are updating themselves and each other based on criteria (it is way longer to describe than I suspect the solution is).

What I end up with is a function which calls itself within a while loop. As you can imagine, this causes a ridiculous amount of recursion.

Here's an example (keeping it short)

var buildArray=firstFunction(new Array(), existingArray)

function firstFunction(thisArray, existingArray){
     for(test1=0; test1<existingArray.length; test1++){
         if(existingArray[test1][3]=='2'){
           secondFunction(thisArray, existingArray, test1);
        }
     }

function secondFunction(thisArray, existingArray, t1){
       for(test2=0; test2<thisArray.length; test2++){
          if(thisArray[test1]<=existingArray[test2][1] || thisArray[test1]>existingArray[test2[0]){
            // do a bunch of stuff to existingArray, now that existingArray has changed, the whole process needs to start again FROM THE BEGINNING!!!
     return firstFunction(new Array(), existingArray);

              // check that the value isn't already in the 'thisArray'
   var check= new Array(existingArray[test1]);
  else if (jQuery.inArray(check, thisArray==-1){
         // value isn't in the new array, so a开发者_开发百科dd it
        thisArray.push(check);
       // thisArray has changed. need to restart the the second function
       secondFunction(thisArray,existingArray);
     }

   }
 }

}
}

I was hoping that

return secondFunction(thisArray, existingArray);

would reset and restart the function, but apparently that isn't happening.

Is there a way to stop the current function and loops and restart with the updated variables?


i do not get what you are trying yo do, however based on the fact that return stop the execution in the secondFunction, and thisArray is never changed, you can add a loop to the firstFunction:

function firstFunction(thisArray, existingArray){
    var restart = true;
    while(restart)
    {
        restart = false;
         for(test1=0; !restart && test1<existingArray.length; test1++){
             if(existingArray[test1][3]=='2'){
               if(secondFunction(thisArray, existingArray, test1))
               {
                restart = true;
               }
            }
         }
    }

and in the secondFunction instead of returning the array return true:

  if(thisArray[test1]<=existingArray[test2][1] || thisArray[test1]>existingArray[test2[0]){
    // do a bunch of stuff to existingArray, now that existingArray has changed, the whole process needs to start again FROM THE BEGINNING!!!
 return true;


Some things I noticed:

  • in secondFunction() you access test1 even though you have a parameter t1
  • the second call to secondFunction() doesn't contain that last parameter
  • the second call to secondFunction() doesn't have a return before it even though you mentioned it in your OP
  • there's a line else if (jQuery.inArray(check, thisArray==-1){, which is missing a ")"
  • there's a line if(thisArray[test1]<=existingArray[test2][1] || thisArray[test1]>existingArray[test2[0]){ missing a "]"
  • in for(test1=0; test1<existingArray.length; test1++){ it should be for(var test1... to let the parser know it's a variable on its own in the recursion (assuming you want this to happen of course)
  • in for(test2=0; test2<thisArray.length; test2++){ it should be for(var test2... to let the parser know it's a variable on its own in the recursion (assuming you want this to happen of course)
  • ...

I'm saying that you edited the code to post it here. That is fine of course, but it makes me believe that some part of the code is missing.

As far as the missing vars is concerned, that may be in your interest, but the parser may get confused. If there's no var the variable is defined at document-level, not at function-level nor at the innermost scope. I don't know if you aware of this or if it's an issue.

0

精彩评论

暂无评论...
验证码 换一张
取 消