I’m just getting started with Javascript and jQuery and have run into a little bit of trouble on my first project. I’m building a simple Blackjack game which works perfectly the first time through.
Once the first hand is over, the game is supposed to reset and play again by calling a recursive playGame() function once a button, #playAgain, is clicked. This works fine, but after playAgain is executed, my hitMe function breaks for the user. It is a systematic code, so both the user and PC are performing the same action, but it is behaving differently for the user and begins to exponentially call the hitMe function based on the number of games played.
Why would this code behave differently in this case based on the user/computer providing the call and how could I go about fixing it?
The code can be found at http://www.calebdwilliams.com/blackjack
Your problem is here inside your playGame function:
You are wiring up a click function to the #hit element every single time you play the game. Well, the subsequent times through the game javascript is just adding additional click handlers. That hander should be added only once either outside the scope of playGame() or else the previous listeners should be removed with: