-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (59 loc) · 1.61 KB
/
script.js
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
62
63
64
let cards = []
let sum = 0
let message = document.getElementById("message")
let total = document.getElementById("total")
let cardsReceived = document.getElementById("cards-received")
let hasBlackJack = false
let isAlive = false
let msg = ""
function getRandom(){
let randomNum = Math.floor(Math.random()*13)+1
if(randomNum > 10){
return 10
}
else if(randomNum === 1){
let randomArr = [1,11]
let randNum = Math.floor(Math.random()*randomArr.length)
return randomArr[randNum]
}
else{
return randomNum
}
}
function start(){
isAlive = true
let firstCard = getRandom()
let secondCard = getRandom()
sum = firstCard + secondCard
cards = [firstCard,secondCard]
render()
}
function render(){
cardsReceived.textContent = "Cards Received: "
for(let i = 0; i < cards.length; i++){
cardsReceived.textContent += cards[i] + " "
}
total.textContent = "Total: " + sum
if (sum <= 20) {
msg = "Do you want to draw a new card?"
message.style.color="blue"
} else if (sum === 21) {
msg = "You've got Blackjack!"
message.style.color="green"
hasBlackJack = true
} else {
msg = "You're out of the game! Start Again!"
message.style.color="red"
message.style.textDecoration="underline"
isAlive = false
}
message.textContent = msg
}
function next(){
if(isAlive === true && hasBlackJack === false){
let card = getRandom()
cards.push(card)
sum += card
render()
}
}