MerynDH
MerynDH

Reputation: 35

call stack increase javascript

I've written a blackjack script that I'd like to iterate recursively until a rather large bankroll runs out. I'd like to run analyses on the telemetry. It's a script that lives locally and poses no danger to anything but the browser environment I'm running it in.

Essentially, the script is supposed to be recursive until the cash runs out. It works fine up to around 5k separate hands or so - for bankrolls up to 10k, and then it throws the max call stack error. However, I need way more data; like > 100k hands.

I've searched SO for solutions and I'm gathering it's a browser-specific thing. Any thoughts would be much appreciated!

Code snippet attached:

function main() {
init();
if (bankRoll >= initialBet) {
    determineBet();
}
else {
    alert("Not enough moneyz to play!");
    console.log("telemetry");
    exitFunction();
}
bankRoll -= initialBet;
playTheGame(); // the whole game, betting, receiving cards, strategy etc
}

Upvotes: 1

Views: 157

Answers (1)

basilikum
basilikum

Reputation: 10536

I suggest you use a loop:

function main() {
    init();
    while (bankRoll >= initialBet) {
        determineBet();
        bankRoll -= initialBet;
        playTheGame(); // the whole game, betting, receiving cards, strategy etc
    }
    alert("Not enough moneyz to play!");
    console.log("telemetry");
    exitFunction();
}

It's hard to say if I refactored it correctly since I don't know what functions like playTheGame or determineBet do, but I hope you get the idea.

Upvotes: 1

Related Questions