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;
}