0
var cards = {
    heart:[2, 3, 4, 5, 6, 7, 8, 9, 10, "joker", "queen", "king", "ace"],
    spade:[2, 3, 4, 5, 6, 7, 8, 9, 10, "joker", "queen", "king", "ace"],
    diamond:[2, 3, 4, 5, 6, 7, 8, 9, 10, "joker", "queen", "king", "ace"],
    king:[2, 3, 4, 5, 6, 7, 8, 9, 10, "joker", "queen", "king", "ace"],
};



function cardSelectionForPlayer(){
    let x = Math.floor((Math.random() * 3) + 0);
    let y = Math.floor((Math.random() * Object.values(cards)[x].length-1) + 0);


    if (y==9 || y==10 || y==11) {
        playerHands.push(10);
    } else if (y==12){
        if ( ((playerHands.reduce((a,b) => a+b, 0))+11) < 21){
            playerHands.push(11);
        } else {
            playerHands.push(1);
        }
    } else {
    playerHands.push(Object.values(cards)[x][y]);2
    }
    delete Object.values(cards)[x][y];
}

I have a deck of cards and every time a player randomly selects a card, the card is removed from that deck. For example, how would I specifically remove number spade 3 from the deck?

Thanks for your time & answer.

Ned
  • 1
  • 1
  • If you know that the numbers are always going to be in a specific position, I.E. 3 is always going to at index 1, then you can just access the key of your choice and remove the element at that index. – tomerpacific May 20 '20 at 11:04

1 Answers1

0

var cards = {
    heart:[2, 3, 4, 5, 6, 7, 8, 9, 10, "joker", "queen", "king", "ace"],
    spade:[2, 3, 4, 5, 6, 7, 8, 9, 10, "joker", "queen", "king", "ace"],
    diamond:[2, 3, 4, 5, 6, 7, 8, 9, 10, "joker", "queen", "king", "ace"],
    king:[2, 3, 4, 5, 6, 7, 8, 9, 10, "joker", "queen", "king", "ace"],
};

var category = "spade";
var value = 3;
var index = cards[category].indexOf(value);

cards[category].splice(index, 1);

console.log(cards);
Shivaji Mutkule
  • 385
  • 1
  • 6
  • 19
  • Thanks for your response. Unfortunately that did not work. – Ned May 20 '20 at 11:55
  • What not worked? It removed 3 from spade. – Shivaji Mutkule May 20 '20 at 11:58
  • I updated the code and I'm sorry for not being clear. That's my fault. Every time I select a card from a deck, how would I remove it? For example, how would I specifically remove number spade 3 from the deck? – Ned May 20 '20 at 13:41