Blackjack

mail@pastecode.io avatar
unknown
javascript
2 years ago
859 B
6
Indexable
Never
const cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11];
const deckOfCards = [...cards, ...cards, ...cards, ...cards];
function getRandomCard() {
  const randomCard = Math.floor(Math.random() * deckOfCards.length);
  const pickedCard = deckOfCards.splice(randomCard, 1)[0];
  return pickedCard;
}
let dealerCards = [];
dealerCards.push(getRandomCard());
dealerCards.push(getRandomCard());
let total = dealerCards[0] + dealerCards[1];
while (true){
  if (total === 21){
    console.log("Blackjack!");
    break;
  }
  else if (total > 21){
    console.log("Bust!");
    break;
  }
  else if(total < 17){
    console.log("Dealer total is " + total + ". Dealer hits!");
    total+=getRandomCard();
  }
  else if(total >=17 && total <= 21){
    console.log("Dealer total is " + total + ". Dealer holds!");
    break;
  }
  
}