Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by javasnip View Post
    My knowledge is limited.. and that is how it was being explained and I personally thought it was me lacking and being confused.. but it seems his way is very awkward.. He was saying that if you named the images that way like 1-J etc. it would show the images instead of the text. But obviously not. does this bit a code from above make sense
    Code:
    cardname1=rands1+"-"+randn1+".jpg";
    Yes, but *ONLY* if you then did something like
    Code:
       document.getElementById("card1").src = cardname1;
    where you ALREADY have, on your page, something like
    Code:
        <img id="card1"/>
    You *could*, instead, do something like
    Code:
        <div id="card1"></div>
        ...
       document.getElementById("card1").innerHTML = '<img src="' + cardname1 + '" alt="random card" />';
    but that's not recommended coding.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  2. #17
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,023
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    By the by...there is a fatal flaw in my code...*AND* in your original code!

    There is *NOTHING* to prevent the SAME CARD from being picked for both cards!!!

    That's because you pick the two cards completely independently of each other.

    The *right* way to do this is to shuffle the deck and then just deal the top two cards off the top of the deck.

    But if you only ever need two cards, it's sufficient to just check the 2nd against the 1st and loop until the 2nd doesn't match the 1st.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  3. #18
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    You don't need images - you could use the HTML codes.
    &hearts &clubs &diams &spades


    Shuffle is probably a bit advanced for you at this stage, but

    Code:
    <script type = "text/javascript">
    var imgArray = new Array('Ace',2,3,4,5,6,7,8,9,10,'Jack','Queen','King'); 
    
    Array.prototype.shuffle = function() {
    var s = [];
    while (this.length) s.push(this.splice(Math.random() * this.length, 1));
    while (s.length) this.push(s.pop());
    return this;
    }
    
    imgArray.shuffle();
    var card1 = imgArray[0];
    alert (card1);
    imgArray.shuffle();
    var card2 = imgArray[0];
    alert (card2);
    
    if (card1 == card2) {
    alert ("The cards match!");
    }
    </script>
    Last edited by Philip M; 12-04-2012 at 08:16 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


 
Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •