How do I handle the recursive call without exceeding the call stack size?
This question was answered here Maximum call stack size exceeded error, but has not been helpful to me. It may be helpful to someone else.
The algorithm prompt is a tad lengthy but for those of you that are determined: coin algorithm
My code may DOES NOT attempt to meet all the requirements, but regardless I'd like some input on what I have.
var gameSetup = {
coins: [2,333,4,11,99,20,10],
minOdds: 1,
maxOdds: 100000
}
function game(gs) {
this.minOdds = gs.minOdds;
this.maxOdds = gs.maxOdds;
var coinArray = gs.coins;
var coinArrayLength = coinArray.length;
var indicesToSplice = [];
var gameResults = {
turns: 0,
score: 0
}
var gameFunctions = {
getTurns: function() {
return gameResults.turns;
},
setTurns: function() {
++gameResults.turns;
},
setScore: function(lcv,rcv) {
var score = lcv * rcv;
gameResults.score += score;
},
getScore: function() {
return gameResults.score;
},
generateFlips: function() {
return generateFlips.getRandomNumbersInclusive();
}
}
var generateFlips = (function() {
var flips = [];
return {
getRandomNumbersInclusive: function() {
flips = [];
for(i=0; i < coinArrayLength; i ++){
var currentFlip = Math.floor(Math.random() * (maxOdds - minOdds + 1)) + minOdds;
flips.splice(0,0,currentFlip);
}
return flips;
}
}
})();
(function takeTurn(coinArrayLength) {
var flips = gameFunctions.generateFlips();
flips.forEach(function(i, index) {
if(i == maxOdds) {
var leftOfIndex = flips[index-1];
var rightOfIndex = flips[index +1];
if(typeof leftOfIndex === 'undefined' || typeof rightOfIndex === 'undefined') {
} else {
indicesToSplice.splice(0,0,index);
var leftCoinValue = coinArray[index-1];
var rightCoinValue = coinArray[index+1];
gameFunctions.setScore(leftCoinValue,rightCoinValue);
}
}
});
if(indicesToSplice.length > 0) {
indicesToSplice.forEach(function(i){
coinArray.splice(i,1);
coinArrayLength = coinArray.length;
});
}
indicesToSplice = [];
gameFunctions.setTurns();
if(coinArrayLength > 2) {
takeTurn(coinArrayLength);
} else {
return;
}
})(coinArrayLength);
}
game(gameSetup);