0

I'm trying to learn about JavaScript, and I'm trying to create a basic comparison game, where I place card numbers in an array, I call 2 of them randomly, and I want to compare which one of the 2 random calls has higher value? Please help me. I've managed to code the program to deal 2 cards at random, I just need a guidance on how to compare the 2 cards drawn.

Thank you.

{
// This is my attempt to create a basic game for a simplified version of ta siau
//where players choose a card, and compares it with dealer's card. Bigger card wins the round

var cards = [2,3,4,5,6,7,8,9,10,"jack","queen","king","ace"]; //available cards in the deck
confirm("start game now?");

var playerCard = cards[Math.floor(Math.random() * cards.length)]; //choose a random card for player

console.log(playerCard);

var dealerCard = cards[Math.floor(Math.random() * cards.length)]; //choose a random card for dealer

console.log(dealerCard);
}
2
  • 1
    @F4r-20 - some sort of order... hmmm.. how about position in an array ;) Commented Jan 9, 2013 at 15:42
  • Kevin will you accept one of these answers. Also NB you didn't specify what happens when the cards are the same so might want to replace player > dealer with player >= dealer if the player wins if it's a draw Commented Jan 9, 2013 at 17:30

4 Answers 4

4

Try this:

if (cards.indexOf(playerCard) > cards.indexOf(dealerCard)) {
   alert("Player wins!");
}
else {
   alert("Dealer wins");
}
Sign up to request clarification or add additional context in comments.

2 Comments

indexof makes more sense since other people haven't noticed "jack","queen","king","ace" aren't numbers
This requires a linear search of each array. They're not very long, in this case, but it's more work than necessary. The index values have been explicitly computed, so instead of this the indexes themselves can be compared.
2

What's the problem?

var playerVal=Math.floor(Math.random() * cards.length);
var playerCard = cards[playerVal];

var dealerVal=Math.floor(Math.random() * cards.length);
var dealerCard = cards[dealerVal]; 

        if(playerVal>dealerVal){
        //player wins
    }

cards is segregated.

2 Comments

What happens when the card is ace, jack, queen? This won't work. The array values will have to be subsituted.
You "edit" input data, instead of provided cards used it indexes.
0

You should work on improving your accept rate, but the primary way you check values against other values is with if

if(playerCard === dealerCard){
    console.log('cards are the same');
} else {
    console.log('cards are different');
}

Note the ===. In most programming languages you test conditionals using ==. In JavaScript, you can do that, but odd things like [] == false will evaluate to true. === requires strict-compliance so [] === false is, indeed, false.

Comments

0

You should operate the card indexes

var playerCardIndex = Math.floor(Math.random() * cards.length)
    var playerCard = cards[playerCardIndex];

    var dealerCardIndex = Math.floor(Math.random() * cards.length)
    var dealerCard = cards[dealerCardIndex]

    if (playerCardIndex > dealerCardIndex) {...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.