Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-03-2012, 11:44 PM   PM User | #16
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-04-2012, 01:32 AM   PM User | #17
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-04-2012, 07:53 AM   PM User | #18
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,098
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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>
__________________

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.

Last edited by Philip M; 12-04-2012 at 08:16 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:38 AM.


Advertisement
Log in to turn off these ads.