Skip to content

Commit

Permalink
Fixed changing bet: isNaN(newBet.value) returned true when there was …
Browse files Browse the repository at this point in the history
…an empty string in the entry field. Javascript tried to convert empty string to a number first which is why there was an issue.
  • Loading branch information
pfra17 committed Oct 8, 2024
1 parent d81b68c commit 3f34b23
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h3>🏦 Balances</h3>
<p>Player: <span id="player_balance"></span></p>
<p>Dealer: <span id="dealer_balance"></span></p>
<h3>🕹️ Game</h3>
<h4>Winner: <span id="winner"></span></h4>
<!-- <h4>Winner: <span id="winner"></span></h4> -->
<!-- <h5>Game phase: <span id="game_phase"></span></h5><br> -->
<div class="container" id="game_cards">
<div id="dealer">
Expand Down
18 changes: 6 additions & 12 deletions scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const standButton = document.getElementById("stand_button");

const pBalanceLabel = document.getElementById("player_balance");
const dBalanceLabel = document.getElementById("dealer_balance");
const winnerElement = document.getElementById("winner");
const pHandDiv = document.getElementById("player_cards");
const dHandDiv = document.getElementById("dealer_cards");
const pHand = document.getElementById("player_hand");
Expand Down Expand Up @@ -108,13 +107,13 @@ class Dealer {
deck.push(CARD_SYMBOLS[card] + CARD_TYPES[3]);
}
console.log("Deck length: " + deck.length);
console.log(deck);
//console.log(deck);
return deck;
}

// Draw a random card from the deck
drawCard(deck) {
console.log(deck);
//console.log(deck);
console.log("Drawing card...")
let randomIndex = Math.floor(Math.random() * deck.length);
let randomCard = deck[randomIndex];
Expand Down Expand Up @@ -246,12 +245,13 @@ p = new Player("", 100, [], 0, 10);

// Change the bet
function changeBet() {
p.bet = parseInt(newBet.value);
console.log(typeof(p.bet));
if (isNaN(p.bet)) {
console.log("changeBet");
console.log(newBet.value);
if (newBet.value === "" || isNaN(newBet.value)) {
alert('Please enter a number!')
} else {
gamePhase = "deal_cards";
p.bet = parseInt(newBet.value);
updateUI();
}
}
Expand Down Expand Up @@ -386,25 +386,19 @@ function displayWinner(winner, blackJack) {
if (blackJack === true) {
document.body.style.backgroundColor = "green";
}
winnerElement.textContent = winner.toUpperCase();
if (winner === "player") {
winnerElement.style.color = "green";
pHandDiv.style.backgroundColor = "green"
dHandDiv.style.backgroundColor = "red"
} else if (winner === "dealer") {
winnerElement.style.color = "red";
pHandDiv.style.backgroundColor = "red"
dHandDiv.style.backgroundColor = "green"
} else {
winnerElement.style.color = "grey";
pHandDiv.style.backgroundColor = "grey"
dHandDiv.style.backgroundColor = "grey"
}
console.log(p.balance, d.balance);
setTimeout(function() {
document.body.style.backgroundColor = "";
winnerElement.textContent = "";
winnerElement.style.color = "";
pHandDiv.style.backgroundColor = ""
dHandDiv.style.backgroundColor = ""
d.initializeGame();
Expand Down
2 changes: 1 addition & 1 deletion style/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ html, body, #new_bet{
}

nav {
background-color: #0d6efd;
background-color: #303336;
color: var(--color);
transition: background-color 0.3s ease, color 0.3s ease;
}
Expand Down

0 comments on commit 3f34b23

Please sign in to comment.