I'm a novice python programmer with 1 day of JS experience trying to make a blackjack text game. My code is supposed to take a card from the deck and place it into the player's hand. When I run the code, it says:
index.js:19 Uncaught TypeError: playerHand.push is not a function
at firstHand (index.js:19)
at index.js:30
The error is at the 19th line which is the line where the player is drawn his second card. Can someone help me fix my code? I'd appreciate it.
Here's the whole JS code:
// initialize the deck of cards
let deck = [
2,3,4,5,6,7,8,9,10,10,10,10,11,
2,3,4,5,6,7,8,9,10,10,10,10,11,
2,3,4,5,6,7,8,9,10,10,10,10,11,
2,3,4,5,6,7,8,9,10,10,10,10,11
];
// shuffle the deck of cards
deck = deck.sort(() => Math.random() - 0.5);
function firstHand(){
//initializing the hands as empty
let playerHand = [];
let dealerHand = [];
//alternate drawing 2 cards for player and dealer
playerHand = playerHand.push(deck.pop());
dealerHand = dealerHand.push(deck.pop());
playerHand = playerHand.push(deck.pop());
dealerHand = dealerHand.push(deck.pop());
//define the total sum in each players hand
let playerSum = playerHand.reduce((a, b) => a + b, 0);
//print the player's hand with first two cards in the hand and the player's total
console.log(`Your hand: ${playerHand} = ${playerSum}`); //for example: "Your hand: [2, 6] = 8"
}
firstHand()