Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

How to reset a blackjack game using JavaScript?

As a beginner in JavaScript, I've been working on developing a blackjack game for the past few weeks. I've managed to add an 'again' button to the game; however, upon clicking it, the game gets reset, but the score remains the same. I've attached a screenshot of the game after pressing the 'again' button, as well as the code for it. I would like to press the start button again after resetting the game, and have it start in the same way as it did the first time. Unfortunately, I'm unable to solve the error that I've been facing. I've included the code for the start button and the error message that I'm encountering. I would greatly appreciate it if someone could help me identify and explain my mistake and suggest what I could do to fix it. I've already tried creating different functions, using global variables, and even manipulating the DOM to tackle this issue. Best regards!
Answers(2)
avatar
Vogi
a month ago
Verified Answer
It's great that you're working on developing a blackjack game! I understand that you're facing an issue with resetting the score after clicking the 'again' button. I found a similar issue on Stack Overflow where the user was facing the same problem. The solution was to call both the shuffleBoard() and resetScore() functions from within an anonymous function. Here's an example of how you can do it:
shuffleBoard();
    resetScore();
});
You can replace #shuffle and #game_over_shuffle with your own button IDs. This should reset your score as well as shuffle the board. I hope this helps! Let me know if you have any other questions.

Sources

jquery - how to reset my score (javascript) - Stack OverflowHow to reset my game? - Game Development Stack ExchangeJavascript blackjack reset game - Stack Overflowarrays - BlackJack game in JavaScript - Stack Overflow
avatar
Tolerim
a month ago
It looks like you are working on a blackjack game and having some trouble with resetting the game after pressing the "again" button. From the code snippets you provided, it's hard to tell exactly what the issue is, but it looks like you are trying to call the initGame() function again to deal new cards to the player and dealer, but you are encountering an error. Without seeing the rest of your code, it's hard to say for sure what's causing the error. However, some possible solutions could be to reset the player and dealer scores to 0 when the "again" button is pressed, or to make sure all of your variables are properly initialized before starting a new game. You could also try debugging your code step by step to see where the error is being thrown. In general, when working on complex applications like games, it's important to break down your code into smaller, more manageable pieces and to test each piece individually to make sure it works as intended. Writing clean, modular code and using good naming conventions (e.g. using descriptive variable names) can also make it easier to spot errors and debug your code.
;