Blackjack the Game! HELLPPP

Tell us what’s happening:
My JS code isn’t running in my game and I’m not sure what the problem is I’ve triple checked my code for any errors and still can’t seem to get the code to run, if anyone has a second to inspect it, it would be nice to have another pair of eyes on this, thanks!

Your code so far

//
// Blackjack
// By Devin Cassidy 
// 


// Card Variables

let suits = ["Hearts", "Clubs", "Diamonds", "Spades"];
let values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight', 'Seven', 'Six', 'Five', 
'Four', 'Three', 'Two'];



// DOM Variables

let textArea = document.getElementById('text-area');
let newGameButton = document.getElementById('new-game-button');
let hitButton = document.getElementById('hit-button');
let stayButton = document.getElementById('stay-button');



// Game Variables 

let gameStarted = false,
    gameOver = false,
    playerWon = false,
    dealerCards = [],
    playerCards = [],
    dealerScore = 0,
    playerScore = 0,
    deck = [];



hitButton.style.display = 'none';
stayButton.style.display = 'none';
showStatus();

newGameButton.addEventListener('click', function(){
  gameStarted = true;
  gameOver = false;
  playerWon = false;
  
  deck = createDeck;
  shuffleDeck(deck);
  dealerCards = [getNextCard(), getNextCard()];
  playerCards = [getNextCard(), getNextCard()];
  
  textArea.innerText = "Started...";
  newGameButton.style.display = 'none';
  hitButton.style.display = "inline";
  stayButton.style.display = "inline";
  showStatus();
});

function createDeck(){
  let deck = [];
  for(let suitIdx = 0; suitIdx < suits.length; suitIdx++){
    for(let valueIdx = 0; valueIdx < values.length; valueIdx++){
      let card = {
        suit: suits[suitIdx],
        value: values[valuesIdx]
      };
        deck.push( card );
    }   
    }
  return deck;
}

let deck = createDeck();

for (let i = 0; i < deck.length; i++ )

let playerCards = [getNextCard(), getNextCard()];

function shuffleDeck(deck){
  for (let i = 0; i < deck.length; i++ ){ let swapIdx = Math.trunc(Math.random() * deck.length);
  let tmp = deck[swapIdx];
  deck[swapIdx] = deck[i];
  deck[i] = tmp;
  }
}

function getCardString(card){
  return card.value + ' of ' + card.suit;
}

function getNextCard(){
  return deck.shift();
}

function getCardNumericValue(card){
  switch(card.value){
    case 'Ace':
      return 1;
    case 'Two':
      return 2;
    case 'Three':
      return 3;
    case 'Four':
      return 4;
    case 'Five':
      return 5;
    case 'Six':
      return 6;
    case 'Seven':
      return 7;
    case 'Eight':
      return 8;
    case 'Nine':
      return 9;
    default:
      return 10;
  }
}

function getScore(cardArray){
  let score = 0;
  let hasAce = false;
  for (let i = 0; i < cardArray.length; i++){
    let card = cardArray[i];
    score += getCardNumericValue(card);
    if (card.value === 'Ace'){
      hasAce = true;
    }
  }
  if (hasAce && score + 10 <= 21){
    return score + 10;
  }
  return score;
}

function updateScores(){
  dealerScore = getScore(dealerCards);
  playerScore = getScore(playerCards);
}

function showStatus(){
  if (!gameStarted){
    text.area.innerText = "Welcome to Blackjack!";
    return;
  }
   
  let dealerCardString = '';
  for (let i=0; i<dealerCards.length; i++){dealerCardString += getCardString(dealerCards[i]) + '\n';
  }
  let playerCardString = '';
  for (let i=0; i<playerCards.length; i++){playerCardString += getCardString(dealerCards[i]) + '\n';
  }
}


updateScores();

textarea.innerText=
'Dealer has:\n' + 
dealerCardString +
'(score: '+ dealerScore + ')\n\n' + 

'Player has:\n' +
playerCardString +
'(score: '+ playerScore + ')\n\n'; 

if (gameOver) {
  if (playerWon) {
    textArea.innerText += "You Win!";
  }
  else {
    textArea.innerText += "Dealer Wins!";
  }
  newGameButton.style.display = "inline";
  hitButton.style.display = "none";
  stayButton.style.display = "none";
}

console.log("Welcome to Blackjack!");

console.log("You are dealt: ");
console.log(" " + getCardString(playerCards[0]) );
console.log(" " + getCardString(playerCards[1]) );


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/

Can you give more details about what you mean by “isn’t running”? Do you have any errors showing in the console? What happens vs. what you expect to happen?

Also, you reference html elements, but you have not provided your html. Do you have a Codepen url for this project you can share?

For some reason my code in JS isn’t executing, they’re no errors in the code but it still won’t execute. Here is a link to my Codepen,

https://codepen.io/DevinCassidy/pen/bJMNNX

You don’t have a code block after this for loop…not sure if that’s the issue.

1 Like

The next line (seen below) is the code block for the for loop.

let playerCards = [getNextCard(), getNextCard()];

The problem is you can only delcare playerCards with a single let. The loop is causing it to try any declare it again and again. If this line is actually meant to be the only line in the code block, then playerCards would need to be declared before the for loop and then just assigned (which it already is). However, there is no reason to reassign different values to playerCards deck.length times.

@DevinCassidy FYI - If you look at your browser’s console, you should see the following error for line 72.

Uncaught SyntaxError: Identifier 'deck' has already been declared
1 Like

So if there is no bracket then does it assume the following line is the code block? Just trying to understand as I’m fairly new to javascript…

Yes, you understand now. The same applies to if statements and other statements.

var x = 7;
var y = 9;

if (x === y)
  console.log('equal');
else
  console.log('not equal');

The above would display ‘not equal’ in the browser’s console.

1 Like

I fixed that error, the game is starting to execute more of the operations I wrote, but still not at 100 percent/fully working at the moment, will post Codepen link when done.

Just update your Codepen so we always have the latest version of what you have tried.

1 Like

My Codepen is updated, they’re no errors in my JS, yet I still can’t seem to initiate the game by hitting the button New Game. If I could get another pair of eyes to look at my code that would be great!

Looks like you got the New Game button working. You still have an error showing in the console you need to deal with regarding dealerCardString not being defined.

It is very interesating