scrimba
Frontend Career Path
Making websites interactive
Blackjack
Assign values in the startGame function
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Assign values in the startGame function
AboutCommentsNotes
Assign values in the startGame function
Expand for more info
index.js
run
preview
console
let firstCard = getRandomCard()
let secondCard = getRandomCard()
let cards = [firstCard, secondCard]
let sum = firstCard + secondCard
let hasBlackJack = false
let isAlive = true
let message = ""
let messageEl = document.getElementById("message-el")
let sumEl = document.getElementById("sum-el")
let cardsEl = document.getElementById("cards-el")


function getRandomCard() {
let randomNumer = Math.floor( Math.random()*13 ) + 1
if (randomNumer > 10) {
return 10
} else if (randomNumer === 1) {
return 11
} else {
return randomNumer
}
}

function startGame() {
// Generate two random numbes
// Re-assign the cards and sum variables so that the game can start
renderGame()
}

function renderGame() {
cardsEl.textContent = "Cards: "
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() {
let card = getRandomCard()
sum += card
cards.push(card)
renderGame()
}
Console
/index.html
-2:42