-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (61 loc) · 1.87 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
// 2. Use getRandomCard() to set the vaues of firstCard and secondCard
let firstCard = getRandomCard()
let secondCard = getRandomCard()
let cards = [firstCard, secondCard] // array - ordered list of items
let sum = firstCard + secondCard
let hasBlackJack = false
let isAlive = true
let message = ""
let messageEl = document.getElementById("message-el")
let sumEl = document.querySelector(".sum-el")
let cardsEl = document.getElementById("cards-el")
// 1. Create a function, getRandomCard()
function getRandomCard() {
let randomNumber = Math.floor( Math.random()*13 ) + 1
if (randomNumber > 10){
return 10
} else if (randomNumber === 1){
return 11
} else{
return randomNumber
}
}
// Create a new function called startGame() that calls renderGame()
function startGame(){
renderGame()
}
function renderGame() {
cardsEl.textContent = " Cards: "
// Creating a for loop that renders out all the cards instead of just two
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() {
// console.log("Drawing a new card from the deck!")
// // 1. Create a card variable, and hard code its value to a number (2-11)
// let card = 7
// // 2. Add the new card to the sum variable
// sum += card
// // 3. Call startGame()
// startGame()
// }
function newCard() {
// Use the getRancomCard() to set the value of card
let card = getRandomCard()
sum += card
cards.push(card)
console.log(cards)
renderGame()
}