-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
59 lines (51 loc) · 1.36 KB
/
Copy pathscript.js
File metadata and controls
59 lines (51 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const player = {
name: "User",
chips: 200,
};
let cards = [];
let sum = 0;
let hasBlackJack = false;
let isAlive = false;
let message = "";
const messageEl = document.getElementById("message-el");
const sumEl = document.getElementById("sum-el");
const cardsEl = document.getElementById("cards-el");
const playerEl = document.getElementById("player-el");
playerEl.textContent = `${player.name}: $${player.chips}`;
function getRandomCard() {
const randomNumber = Math.floor(Math.random() * 13) + 1;
return randomNumber > 10 ? 10 : randomNumber === 1 ? 11 : randomNumber;
}
function startGame() {
isAlive = true;
const firstCard = getRandomCard();
const secondCard = getRandomCard();
cards = [firstCard, secondCard];
sum = firstCard + secondCard;
renderGame();
}
function renderGame() {
cardsEl.textContent = "Cards: " + cards.join(" ");
sumEl.textContent = "Sum: " + sum;
if (sum <= 20) {
message = "Do you want to draw a new card?";
} else if (sum === 21) {
message = "You've got Blackjack!";
hasBlackJack = true;
} else {
message = "You're out of the game!";
isAlive = false;
}
messageEl.textContent = message;
}
function newCard() {
if (isAlive && !hasBlackJack) {
let card = getRandomCard();
if (card === 11 && sum + card > 21) {
card = 1;
}
sum += card;
cards.push(card);
renderGame();
}
}