I am developing a blackjack game. everything has been working so far besides for this little detail. Basically the start game button should only be visible before a game and after a game. I have no problem with making the visibility hidden but it does not work the other way around when I finish a game.
function startGame() {
startGameButton.style.visibility = "hidden";
if (!player.playerName) {
player.playerName = prompt("What's your name?")
player.chips = 100
playerEl.textContent = "Player: " + player.playerName;
}
if (cards.length < 2) {
let bet = prompt('how much do you want to bet (chips: ' + player.chips + ')')
player.chips = player.chips - bet;
chipsEl.textContent = "Chips: " + player.chips;
}
let firstCard = randomCard();
let secondCard = randomCard();
cards = [firstCard, secondCard];
let tableFirstCard = randomCard();
let tableSecondCard = randomCard();
tableCards = [tableFirstCard, tableSecondCard];
let message = "";
let hasBlackJack = false;
let isAlive = true;
sum = firstCard + secondCard;
newCardButton.style.visibility = "visible";
startGameButton.textContent = "New Game";
if (sum < 21) {
message = "Do you want to draw a new card?";
} else if (sum === 21) {
message = "You've got Blackjack!";
newCardButton.style.visibility = "hidden";
startGameButton.style.visibility = "visible";
player.chips = player.chips + bet * 2
hasBlackJack = true;
} else {
startGameButton.style.visibility = "visible";
isAlive = false;
message = "Bad luck! You're out of the game!";
}
messageEl.textContent = message;
cardsEl.textContent = "Cards: " + firstCard + " " + secondCard;
tableCardsEl.textContent = "Table Cards: " + tableSecondCard + " " + tableSecondCard;
sumEl.textContent = "Sum: " + sum;
console.log("blackjack: " + hasBlackJack);
console.log("is Alive: " + isAlive);
}
I have tried to even switch it around to see if it makes any difference all that happens in:
if (sum < 21) {
message = "Do you want to draw a new card?";
} else if (sum === 21) {
message = "You've got Blackjack!";
newCardButton.style.visibility = "hidden";
startGameButton.style.visibility = "visible";
player.chips = player.chips + bet * 2
hasBlackJack = true;
} else {
startGameButton.style.visibility = "visible";
isAlive = false;
message = "Bad luck! You're out of the game!";
}
everything in the if statement is happening aside from the visibility part.
if you need more context here is a link to the code: https://github.com/pedrosilva410/blackjack-game