Reputation: 135
I am trying to build a Blackjack game in Javascript and wanting to determine if the Player's hand contains an "Ace".
I have an array for the Player's hand and Dealer's hand to contain all the cards, each card object has 3 values associated with it...the cards name, suit and value. How do you look specifically for the value "Ace" for each object in the array?
function card(name, suit, value) {
this.name = name;
this.suit = suit;
this.value = value;
}
var playerHand = [];
var drawOne = function() {
var card = cardsInDeck.pop();
return card;
}
var p1 = drawOne();
playerHand.push(p1.value);
var p2 = drawOne();
playerHand.push(p2.value);
if ((playerHand.indexOf("Ace") > 0) && playerTotal > 21) {
playerTotal -= 10;
}
Any insight into how to approach this correctly would be appreciated.
Upvotes: 0
Views: 65
Reputation: 68393
I have an array for the Player's hand and Dealer's hand to contain all the cards, each card object has 3 values associated with it.
Assuming playerHand
is an array, you can try
var hasAce = playerHand.some( function( card ){
return card.suite.toLowerCase() == "ace";
});
Now just proceed with your rest of the logic
if ( hasAce && playerTotal > 21) {
playerTotal -= 10;
}
Edit
Based on the updates shared by you, since you are only pushing the value rather than the object, indexOf
will work just fine
if ((playerHand.indexOf("Ace") > 0) && playerTotal > 21) {
playerTotal -= 10;
}
Upvotes: 2