-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (56 loc) · 1.57 KB
/
index.js
File metadata and controls
61 lines (56 loc) · 1.57 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
60
61
let player = {
name : "Mayowa",
chips : 10
}
let cards = [random(), random()]
let message = document.getElementById("message-el")
let cardsEl = document.getElementById("cards-el")
let sum = document.getElementById("sum-el")
let isAlive = true
let hasBlackJack = false
let game = cards[0] + cards[1]
let playerEl = document.getElementById("player-El")
playerEl.textContent = player.name + ": $" + player.chips
function random(){
let randonNum = Math.floor(Math.random() * 13) + 1
if (randonNum === 1){
return 11
} else if (randonNum > 10){
return 10
} else{
return randonNum
}
}
function startGame(){
playerEl.textContent = player.name + ": $" + (player.chips - 2)
renderGame()
}
function renderGame(){
cardsEl.textContent = "Cards: "
for(i = 0; i < cards.length; i++){
cardsEl.textContent += cards[i] + " "
}
if (game <= 20){
message.textContent = "Do you want to draw another card?"
sum.textContent = "Sum: " + game
} else if(game === 21){
message.textContent = "You have won a Blackjack"
sum.textContent = "Sum: " + game
hasBlackJack = true
} else{
message.textContent = "You have lost the game"
sum.textContent = "Sum: " + game
isAlive = false
}
}
function newCard(){
if (isAlive === true && hasBlackJack === false){
cards.push(random())
game += cards[2]
cardsEl.textContent += ", " + cards[2]
renderGame()
}
else{
message.textContent = "You can't draw a new card, start a new game?"
}
}