-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (63 loc) · 1.73 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
let sum= 0
let cards= []
let hasBlackJack = false
let isAlive = false
let messageEl = document.getElementById("message-el")
let sumEl = document.querySelector("#sum-el")
let cardsEL = document.querySelector("#cards-el")
let player = {
name:"Harris",
chips:135
}
let playerEL = document.querySelector("#player-el")
playerEL.textContent = (player.name+" "+player.chips)
function renderGame(){
cardsEL.innerText = "Cards: "
for(let i = 0;i<cards.length;i++){
cardsEL.textContent+=cards[i]+ ", "
}
if(sum===21){
message = "congrats! You have won Blackjack!"
hasBlackJack = true
}else if(sum<21){
message = "Would you like another card?"
}else{
message = "You are out of the game!"
isAlive =false
}
sumEl.innerText = "Sum:"+sum
messageEl.innerText = message;
}
function startGame(){
let firstcard = getRandomCard()
let secondcard = getRandomCard()
cards = [firstcard,secondcard]
sum = firstcard + secondcard
isAlive = true
hasBlackJack = false
renderGame()
}
function newCard(){
if(isAlive===true&&hasBlackJack===false){
let newCard = getRandomCard();
sum+=newCard;
cards.push(newCard)
renderGame();
}
}
function getRandomCard(){
let card = Math.random()*13;
card = Math.floor(card)+1
if(card===1){
// let answer=""
// while(answer!=="1"||answer!=="11"){
// answer = window.prompt("Would you like this 1 or 11?","1");
// }
// return parseInt(answer)
return 11
}else if(card>10){
return 10
}
console.log(card)
return card
}