Did use js,html and some css to build a blackljack game.
// object
let player = {
name: "ben",
chips: 200,
sayHello: function () {
console.log("Heisann!");
},
};
player.sayHello();
let cards = []; // array -ordered list of item
let sum = 0;
let hasBlackJack = false;
let isAlive = false;
let message = "";
let messageEl = document.getElementById("message-el");
let sumEl = document.querySelector("#sum-el");
let cardsEl = document.querySelector("#cards-el");
let playerEl = document.getElementById("player-el");
playerEl.textContent = player.name + ": $" + player.chips;
// Create a function, getRandomCard(), that always returns the number 5
// FLOR REMOVE DECIMALS
function getRandomCard() {
// if 1 -> return 11
// if 11-13 -> return 10
let randomNumer = Math.floor(Math.random() * 13) + 1;
if (randomNumer > 10) {
return 10;
} else if (randomNumer === 1) {
return 11;
} else {
return randomNumer;
}
}
function startGame() {
isAlive = true;
let firstCard = getRandomCard();
let secondCard = getRandomCard();
cards = [firstCard, secondCard];
sum = firstCard + secondCard;
renderGame();
}
function renderGame() {
cardsEl.textContent = "Cards: ";
for (let i = 0; i < cards.length; i++) {
cardsEl.textContent += cards[i] + " ";
}
sumEl.textContent = "sum: " + sum;
if (sum <= 20) {
message = "Do you want to draw a new card? 🙂";
} else if (sum === 21) {
message = "Wohoo! You've got Blackjack! 🥳";
hasBlackJack = true;
} else {
message = "You're out of the game! ðŸ˜";
isAlive = false;
}
messageEl.textContent = message;
}
function newCard() {
// Only allow the player to get a new card if she IS alive and does NOT have Blackjack
if (isAlive === true && hasBlackJack === false) {
let card = getRandomCard();
sum += card;
cards.push(card);
renderGame();
}
}
```
Top comments (0)