Home > database >  Blackjack Javascript: Remove item in an object or an array
Blackjack Javascript: Remove item in an object or an array

Time:05-18

I have completed a Blackjack tutorial, with some issues left unsolved in it (for tutorial purposes maybe).

I am attempting to remove a card object from a collection once that card has been dealt.

Here is the data/object code I have been using throughout the tutorial:

let blackjackGame = {
    'you': {"scoreSpan": '#your-blackjack-result', 'div': '#your-box', 'score': 0},
    'dealer': {"scoreSpan": '#dealer-blackjack-result', 'div': '#dealer-box', 'score': 0},
    'cards': ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'K', 'J', 'Q', 'A'],
    'cardsMap': {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'K': 10, 'J': 10, 'Q': 10, 'A': [1, 11]}, }; 

I have tried using the delete() method for the blackjackGame objects' cards array and the cardsMap object on my deal() functions here:

function blackjackDeal() {
    if (blackjackGame['turnsOver'] === true) {
        blackjackGame['isStand'] = false;
        let yourImages = document.querySelector('#your-box').querySelectorAll('img');
        let dealerImages = document.querySelector('#dealer-box').querySelectorAll('img');
        for (i=0; i < yourImages.length; i  ) {
            yourImages[i].remove();
        }
        for (i=0; i < dealerImages.length; i  ) {
            dealerImages[i].remove();
        }
        YOU['score'] = 0;
        DEALER['score'] = 0;
        document.querySelector('#your-blackjack-result').textContent = 0;
        document.querySelector('#dealer-blackjack-result').textContent = 0;
        document.querySelector('#your-blackjack-result').style.color = 'white';
        document.querySelector('#dealer-blackjack-result').style.color = 'white';
        document.querySelector('#blackjack-result').textContent = "Let's play!";
        document.querySelector('#blackjack-result').style.color = 'black';
        blackjackGame['turnsOver'] = true;
    }
 }

And the dealerLogic() function:

async function dealerLogic() {
    blackjackGame['isStand'] = true;
    while(DEALER['score'] < 16 && blackjackGame['isStand'] === true) {
        let card = randomCard();
        let copy = card;
        showCard(copy, DEALER);
        updateScore(copy, DEALER);
        showScore(DEALER);
        await sleep(1000);
    }
    blackjackGame['turnsOver'] = true;
    let winner = computeWinner();
    showResult(winner);
}

You will notice I have tried to make copies of the blackjackGame object and array, but the cards don't go anywhere.

All the code logic in these two functions and the blackjackGame variable works fine, this part wasn't covered in the tutorial (which is fine), I'm just very interested in how to do this, as I did years ago with a lottery prediction machine in JavaScript (a long time ago).

Any help would be greatly appreciated.

CodePudding user response:

Did you concider adding a property to the cards that specifies if the card had been delt or not ? for example

let blackjackGame = {
    'you': {"scoreSpan": '#your-blackjack-result', 'div': '#your-box', 'score': 0},
    'dealer': {"scoreSpan": '#dealer-blackjack-result', 'div': '#dealer-box', 'score': 0},
    'cards': [
      {"card": 2, "isDealt": false},
      {"card": 3, "isDealt": false},
      {"card": 4, "isDealt": false},
      // the other cards ....
      {"card": 'A', "isDealt": false}
      ],
    'cardsMap': {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'K': 10, 'J': 10, 'Q': 10, 'A': [1, 11]}, }; 

And whenever a card is dealt, you change the property isDealt to true. And only serve cards that fulfill the condition blackjackGame.cards[i].isDealt === false.

Hope this helps.

CodePudding user response:

The answer helped me in the right direction.

let blackjackGame = {
    'hearts': {"card2": 2, "isDealt": false, "card3": 3, "isDealt": false, "card4": 4, "isDealt": false,
               "card5": 5, "isDealt": false, "card6": 6, "isDealt": false, "card7": 7, "isDealt": false,
               "card8": 8, "isDealt": false, "card9": 9, "isDealt": false, "card10": 10, "isDealt": false,
               "cardK": 10, "isDealt": false, "cardJ": 10, "isDealt": false, "cardQ": 10, "isDealt": false,
               "cardA": [1, 11]},
    
};
console.log(delete(blackjackGame['hearts']['card2']));

It outputs to true on the console, thus deleting the property of the JavaScript object.

  • Related