-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
113 lines (90 loc) · 2.61 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//Black Jack Game
//let firstCard = getRandomCard() //Variables
//let secondCard = getRandomCard() //Variables
//Player objects with values
let player = {
name: "Puso",
chips: 300
}
let cards = [] //Variables | This was changed and added to the startGame Function
let sum = 0 //Variables | By default the number on the sum of cards should be zero before you generate the cards
let hasBlackJack = false //Booleans
let isAlive = false //Booleans | Must be set to false(default) because you have not started game yet
let message = " " //Empty String To Be Added At Functions
//Do you want to draw card message
let messageEl = document.getElementById("message-el") //Variables
//Accumulated card number you have in game
let sumEl = document.getElementById("sum-el") //Variables
//Cards JavaScript selector variable
let cardsEl = document.getElementById("cards-el") //Variables
let playerEl = document.getElementById("player-el")
playerEl.textContent = player.name + ": R" + player.chips
//Creating Randomness For Cards Drawn
function getRandomCard() {
let randomNumber = Math.floor( Math.random() * 13 ) + 1
if ( randomNumber > 10 ) {
return 10
} else if ( randomNumber === 1 ) {
return 11
} else {
return randomNumber
}
}
function startGame() {
isAlive = true
let firstCard = getRandomCard()
let secondCard = getRandomCard()
cards = [firstCard, secondCard]
sum = firstCard + secondCard
renderGame()
}
function renderGame() {
cardsEl.textContent = "Cards: "
//Added loop for displaying array - very important for me to remeber
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 = "You've got BlackJack!"
hasBlackJack = true
}
else {
message = "You're out of the game!"
isAlive = false
}
messageEl.textContent = message
}
function newCard() {
if ( isAlive === true && hasBlackJack === false) {
let card = getRandomCard()
sum += card
//Pushed card to array
cards.push(card)
renderGame()
}
}
/*
//Club Admission
let age = 22
if (age < 21) {
console.log("You cannto enter club!")
}
else {
console.log("Welcome!")
}
//Norway King Letter
let age = 100
if (age < 100) {
console.log("Not elegible")
}
else if (age === 100) {
console.log("Here is your birthday card from the King!")
}
else {
console.log("Not elegible, you already got one!")
}
*/