0

I have a main function that calls a second function which contains a for loop that calls a third function multiple times. The third function returns a value to the for loop each iteration and that value is pushed to an empty array which is declared outside the for loop. The third function also calls two other functions to get this done. ALL functions work perfectly well on their own.

Ultimately, when the for loop in the second function is finished, the second function returns the populated array back to the first function. Unfortunately, no matter what i do, the second function only iterates the for loop once and then somehow breaks out of it on its own and still returns the single value that the third function was able to return.

The for loop does call the third function successfully the one time and gets the return value, and pushes it to the array, which then does get successfully returned back to the first function. Why is the for loop in the second function only able to call the third function once and then seems to break out of itself automatically of it's own accord?

I have tested with three very simple functions, and i have no issues with the for loop breaking out after the first iteration.

within first function, calling second function and sending two predefined parameters:

    var flavsCosts = calcFlavCost(batchNum,chosenFlavors);

second function:

    function calcFlavCost(batchNum,chosenFlavors) {

    var allBatchFlavsCosts = [];

      for (i=0;i<chosenFlavors.length;i++)  // chosenFlavors.length >= 2
      {
      /* 
        iterates thru chosenFlavors, and creates unique
        variables thisFlav and thisNumKegs each iteration,
        then calls third function: 
      */
      var thisFlavCost = getFlavCost(thisFlav,thisNumKegs);   
      allBatchFlavsCosts.push(thisFlavCost);  
      }
    return allBatchFlavsCosts;
    }

third function:

    function getFlavCost(thisFlav,thisNumKegs) {
    // calls a function getRecipe(), which returns 2d array
    // calls a function getUnitCosts(), which returns simple array
    // executes a for loop for some math stuff
    // returns total cost to function 2

    return materialCosts;
    }

  • Welcome to StackOverFlow please take this opportunity to take the [tour] and learn how to [ask], [format code](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks), [mcve] and [Tag Info](https://stackoverflow.com/tags/google-apps-script/info) – Cooper May 07 '20 at 02:24
  • `thisflav` and `thisNumKegs` is undefined – Cooper May 07 '20 at 02:25
  • hi, thanks for getting back.. i left out any unnecessary code to keep it brief. they are not important, but they are defined within the second function before calling getFlavCost function. i updated the code to reflect that. thx – theunspeakablethings May 07 '20 at 04:01
  • i am more interested in if there is an issue with the depth of calling functions from within other functions? will this cause a for loop to break out? – theunspeakablethings May 07 '20 at 04:03
  • i just created a very simple version with very simple function calls and it works perfectly. so perhaps my issue does lie within variable declaration or something small. going to edit the above code to include a little bit more of the specifics to help others troubleshoot. perhaps it is just a small mistake i can't locate. – theunspeakablethings May 07 '20 at 04:23
  • I am trying your code, but it's working for me. Check with a ```Logger.log(chosenFlavors.length)``` the length of your array to verify it really has a length longer than 2 – alberto vielma May 07 '20 at 09:20
  • I'm afraid you'll have to post more of the code. My guess would be something messing up along the way, causing null values to be pushed to the array, but can't tell without better detail. – muds May 07 '20 at 12:09
  • @albertovielma yes, already checked. length is always longer than 2 – theunspeakablethings May 07 '20 at 14:03
  • ok, by changing all other for loops in each of the different functions in question to be other than "i", it fixed the problem and the loop iterates properly. see the above recommended related question for more details. i still don't really get WHY though. somehow "i" is being confused within different functions even though it isn't being passed between functions, so that makes no sense to me. if you are using "i" in a for loop in one function and then you call another function that has a for loop and also uses "i", those are completely separate functions/loops aren't they? – theunspeakablethings May 07 '20 at 14:18
  • Checking the answer in the other post, I can see you just forgot to set scope of your variable, therefore Apps Script was taking the ```i``` as global variable – alberto vielma May 07 '20 at 14:40
  • Read the link in the other post – TheMaster May 07 '20 at 14:53
  • @TheMaster yes, i did, even with that being said, it still says it would be global to a particular function, not to all functions... i have different for loops but they are in different functions entirely. obviously still missing something. ugh – theunspeakablethings May 07 '20 at 15:38
  • Could you quote the exact words in the documentation? Where exactly do you see that? – TheMaster May 07 '20 at 15:43
  • Related: https://stackoverflow.com/q/6888570 – TheMaster May 07 '20 at 16:39
  • @TheMaster that related link was very helpful, but still confusing. mainly because my personal experience is that i've initialized i=0 within for loops all over the place in code previously (since i never had any of this knowledge about it being a global variable that is being created) and never had this issue. in regards to the first linked documentation, maybe i misunderstood, but it seemed it was saying that the variable initialized in the for loop would be global to that particular function containing the for loop, not every function in the entire script file. – theunspeakablethings May 08 '20 at 16:15

0 Answers0