View Full Version : Javascript Help please!!

11-30-2012, 08:30 PM
Hey all! I am having some trouble with my program for blackjack; my variables a,b,c and d will not store values from randomCard ( I use them later in the code to print out a card image on the screen). Does anyone see anything I might be overlooking? (randomCard DOES get a value from Deal(), which is a random number generator, I know this because randomC() utilizes randomCard).

Thanks for your time!

function NewGame(){
player.length = 0;
dealer.length = 0;
playerTotal = 0;
dealerTotal = 0;

randomCard = Deal();
a = randomCard;
player [0] = randomC();

randomCard = Deal();
b = randomCard;
player [1] = randomC();

randomCard = Deal();
c = randomCard;
dealer[0] = randomC();

randomCard = Deal();
d = randomCard;
dealer[1] = randomC();

playerTotal = player [0] + player [1];
dealerTotal = dealer [0] + dealer [1];
document.form.player.value = playerTotal;
document.form.dealer.value = dealerTotal;

11-30-2012, 09:22 PM
I am putting the variables in the code shown below, if I enter a [0] or [1] the correct image shows up, but I need it to coincide with the the random variable instead of 0 and 1.

<form NAME="form">
<table height="165">
<td height="137"><table width="814">
<td><script>document.write('<img alt="random image" src="' + playerC[a] + '"/>');</script>
<script>document.write('<img alt="random image" src="' + playerC[b] + '"/>');</script></td>
<td><script>document.write('<img alt="random image" src="' + playerC[c] + '"/>');</script>
<script>document.write('<img alt="random image" src="' + playerC[d] + '"/>');</script></td>

Old Pedant
11-30-2012, 10:03 PM
This makes NO SENSE at all:

randomCard = Deal();
a = randomCard;
player [0] = randomC();

You get a randomCard. Fine. Then, for some weird reason, you assign it to the variable a

Why didn't you simply code a = Deal();?

But then you set player[0] to the result of calling the function randomC()!


*APPARENTLY* you have a Deal( ) function that returns one random card. That's great. But then why do you have *ANOTHER* function randomC() that does the same thing????

On top of that, you get the score for the player by adding player[0] and player[1]. But why in the world would *THAT* score match the sum of the values of a and b??? Except by the wildest coincidence?


In your second post, you show this:

document.write('<img alt="random image" src="' + playerC[a] + '"/>');

Now, all of a sudden, you have introduced the array playerC[].

Where did *THAT* come from?

In the prior code, you had player[0] and player[1], but you certainly didn't have any playerC[ ].

On top of everything else, document.write is badly obsolete and you should *NOT* be using it.

11-30-2012, 10:47 PM
Deal() returns a value 0-51.
randomC() takes the RANDOMCARD and assigns it a card VALUE based on a case switch statement.

playerC[] are the cards, I have an array set up with 0-51 assigning a different card in the deck.

a & randomCard must be equal numbers so I assign the correct card, that is why I do not simply code a = Deal().

If document.write is so obsolete why not inform me of the appropriate code?

Thank you, I think.

Old Pedant
11-30-2012, 11:09 PM
Okay...begins to make more sense.

Your playerC is fine, though I would have used a more descriptive name. After all, you use those images for the dealer's cards, as well. Maybe cardImage[ ]?

But that's quibbling.

randomC() takes the RANDOMCARD and assigns it a card VALUE based on a case switch statement.
Bad design. (Also misleading name.)

I would have written it as

function cardValue( card )

NEVER depend upon passing global variables around from function to function if you can help it!

If you were going to do that, you could have had Deal( ) set a global variable with the card's value. But even that is bad design.


In place of document.write, you should use DOM methods.

For example:

<td><img id="PC1" alt="player card 1"/></td>
<td><img id="PC2" alt="player card 2"/></td>
<td><img id="DC1" alt="dealer card 1"/></td>
<td><img id="DC2" alt="dealer card 2"/></td>


<script type="text/javascript">
function dealTo( whom, imagePrefix )
var total = 0;
for ( var c = 0; c <= 1; ++c )
var card = Deal();
total += cardValue( card );
document.getElementById( imagePrefix + (c+1) ).src = playerC[card]; // I'd still rename playerC
whom[c] = card;
return total;

function newGame( )
playerCards = [];
dealerCards = []; // better than setting length to zero...start with clean array

playerTotal = dealTo( playerCards, "PC" );
dealerTotal = dealTo( dealerCards, "DC" );
... other code ...

Yes, you should put the JS code *AFTER* all the HTML, just before the </body> tag.

Old Pedant
11-30-2012, 11:13 PM
By the by, there is no need for a switch( ) in your cardValue( ) function (what you were calling playerC( ).

Assuming that 0-12 are the Ace through King of Clubs, 13-25 are Ace through King of Diamonds, etc.:

function cardValue( card )
card %= 13; // reduce to 0 through 12
if ( card >= 10 ) { return 10; } /* J, Q, K */
if ( card == 0 ) { return 11; } /* Ace */
return card+1; // all other cards

If you use some other numbering system for the card images, you can still do it without switch( ), almost surely.

Old Pedant
11-30-2012, 11:19 PM
Oh...and named <form>s are also obsolete. Give the form an ID, instead, and then use

var form = document.getElementById("theForm");

or similar to refer to it.

12-01-2012, 01:19 AM
Thank you so very much for your help! This cleared up a lot and I have the images showing with appropriate values. :thumbsup:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum