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/

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

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…

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.

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!

It is very interesating