# Problem with Letters and Numbers in Blackjack Strategy Program

Page 1 of 1

## 8 Replies - 941 Views - Last Post: 25 June 2012 - 09:56 PM

### #1 USC2587

• New D.I.C Head

Reputation: 1
• Posts: 29
• Joined: 04-June 12

# Problem with Letters and Numbers in Blackjack Strategy Program

Posted 22 June 2012 - 04:26 PM

Hi. I'm trying to make a Blackjack Strategy Trainer, where the player inputs his hand and the dealer's hand. I want the program to output the correct play, such as hit, stand, or double.

The problem I'm having is with Aces. My code sends it straight to the else statement, which is not correct.

Here is the link to the Blackjack Strategy Chart:
http://www.blackjackinfo.com/bjbse.php

```<head>
<title>Exercises for If Statements</title>
</head>
<body>
<script type="text/Javascript">
<!--

var linebreak = "<br/>"

var Y = parseFloat( prompt("Enter your hand","") );

var D = parseFloat( prompt("Enter the dealer's hand", "") );

if (Y <= "8"){
document.write("Hit<BR>");
}
else if(Y == "9" && (D == "2" || D == "7" || D == "8" || D =="9" || D == "10" || D == "A")){

document.write("Hit<BR>");
}
else if(Y == "9" && (D == "3" || D == "4" || D == "5" || D == "6")){
document.write("Double<BR>");
}

else if(Y == "10" && (D == "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9")){

document.write("Double<BR>");
}
else if(Y == "10" && (D == "10" || "A")){
document.write("Hit<BR>");
}
else{
document.write("This hand does not exist.");
}

//-->
</script>

</body>
</html>
```

*** Edit ***
Please use code tags when posting code!

This post has been edited by GunnerInc: 22 June 2012 - 04:28 PM
Reason for edit:: Added code tags

Is This A Good Question/Topic? 0

## Replies To: Problem with Letters and Numbers in Blackjack Strategy Program

### #2 Atli

• Enhance Your Calm

Reputation: 4013
• Posts: 6,800
• Joined: 08-June 10

## Re: Problem with Letters and Numbers in Blackjack Strategy Program

Posted 22 June 2012 - 04:38 PM

One problem I see there is that you are incorrectly using the or (||) operator in your last two else if statements. - An OR operator does not take into account which variables you were using in a previous condition. You must explicitly specify the condition in each part of a multi-part OR condition. - Each part is itself a unique condition, not an add-on to previous conditions.

That is:
```var x = "A";

// This is incorrect. It will always log "true", because
// by themselves the "C" and "D" conditions are always true.
// (They exists, therefore Javascript considers them true.)
if (x == "B" || "C" || "D") {
console.log("true");
}
else {
console.log("false");
}

// You need to specify what variable they are matching in
// each part of the statement.
if (x == "B" || x == "C" || x == "D") {
console.log("true");
}
else {
console.log("false");
}

```

This post has been edited by Atli: 22 June 2012 - 04:40 PM

Was This Post Helpful? 2

### #3 USC2587

• New D.I.C Head

Reputation: 1
• Posts: 29
• Joined: 04-June 12

## Re: Problem with Letters and Numbers in Blackjack Strategy Program

Posted 22 June 2012 - 05:25 PM

Atli, on 22 June 2012 - 04:38 PM, said:

One problem I see there is that you are incorrectly using the or (||) operator in your last two else if statements. - An OR operator does not take into account which variables you were using in a previous condition. You must explicitly specify the condition in each part of a multi-part OR condition. - Each part is itself a unique condition, not an add-on to previous conditions.

That is:
```var x = "A";

// This is incorrect. It will always log "true", because
// by themselves the "C" and "D" conditions are always true.
// (They exists, therefore Javascript considers them true.)
if (x == "B" || "C" || "D") {
console.log("true");
}
else {
console.log("false");
}

// You need to specify what variable they are matching in
// each part of the statement.
if (x == "B" || x == "C" || x == "D") {
console.log("true");
}
else {
console.log("false");
}

```

I was wondering if I that was allowed...Thanks for pointing that out to me.

I don't understand when you put... var x ="A"

I need for "A" to be part of the "dealer's hand" (D). It seems the computer isn't picking up letters. As I get further with this code, I'll also need to have "A" to be part of "your hand" (Y). Thanks for the help.
Was This Post Helpful? 0

### #4 Atli

• Enhance Your Calm

Reputation: 4013
• Posts: 6,800
• Joined: 08-June 10

## Re: Problem with Letters and Numbers in Blackjack Strategy Program

Posted 22 June 2012 - 05:33 PM

USC2587, on 23 June 2012 - 12:25 AM, said:

It seems the computer isn't picking up letters.

It is. The problem is that you are specifically asking it to convert the input to a number:
```var Y = parseFloat( prompt("Enter your hand","") );

var D = parseFloat( prompt("Enter the dealer's hand", "") );

```

The parseFloat function tells Javascript to try and convert the input to a float number. If that fails, it will return "NaN" (not a number) instead.

Why are you using the parseFloat function there? I can't see any reason for it, especially seeing as you are only ever dealing with whole numbers, aside from the letters. - I suggest you remove that altogether.
Was This Post Helpful? 1

### #5 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• Joined: 16-October 07

## Re: Problem with Letters and Numbers in Blackjack Strategy Program

Posted 23 June 2012 - 04:54 AM

You have a lovely little chart there; any reason you aren't just storing that to look up?

As a user of your page I can only conclude you hate me. Why not give me a couple of drop downs? Why make me enter text that can be invalid crap?
Was This Post Helpful? 1

### #6 USC2587

• New D.I.C Head

Reputation: 1
• Posts: 29
• Joined: 04-June 12

## Re: Problem with Letters and Numbers in Blackjack Strategy Program

Posted 24 June 2012 - 03:45 PM

Atli, on 22 June 2012 - 05:33 PM, said:

USC2587, on 23 June 2012 - 12:25 AM, said:

It seems the computer isn't picking up letters.

It is. The problem is that you are specifically asking it to convert the input to a number:
```var Y = parseFloat( prompt("Enter your hand","") );

var D = parseFloat( prompt("Enter the dealer's hand", "") );

```

The parseFloat function tells Javascript to try and convert the input to a float number. If that fails, it will return "NaN" (not a number) instead.

Why are you using the parseFloat function there? I can't see any reason for it, especially seeing as you are only ever dealing with whole numbers, aside from the letters. - I suggest you remove that altogether.

Ok. I just saw it used in an example. The tutorial I'm currently using doesn't go into detail about how to get numbers and letters. What kind of function does this action?

baavgai, on 23 June 2012 - 04:54 AM, said:

You have a lovely little chart there; any reason you aren't just storing that to look up?

As a user of your page I can only conclude you hate me. Why not give me a couple of drop downs? Why make me enter text that can be invalid crap?

I'm making this to practice my if statements and boolean logic. I don't know how to make it read numbers and letters (Aces). If I could figure this out, then the rest will be a breeze.
Was This Post Helpful? 0

### #7 USC2587

• New D.I.C Head

Reputation: 1
• Posts: 29
• Joined: 04-June 12

## Re: Problem with Letters and Numbers in Blackjack Strategy Program

Posted 25 June 2012 - 02:49 AM

```<html>
<head>
<title>Blackjack Strategy Trainer</title>
</head>
<body>
<script type="text/Javascript">
<!--

var linebreak = "<br/>"

var Y = prompt("Enter your hand","");

var D = prompt("Enter the dealer's hand", "");

if (Y <= "8")
{
document.write("Hit<BR>");
}
else if(Y == "9" && (D == "2" || D == "7" || D == "8" || D =="9" || D == "10" || D == "A"))
{

document.write("Hit<BR>");
}
else if(Y == "9" && (D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}

else if(Y == "10" && (D == "2" || D =="3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9"))
{

document.write("Double<BR>");
}
else if(Y == "10" && (D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "11" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10"))
{
document.write("Double<BR>");
}
else if(Y == "11" && D == "A")
{
document.write("Hit<BR>");
}

else if(Y == "12" && (D == "2" || D == "3" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "12" && (D == "4" || D =="5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "13" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Stand<BR>");
}
else if(Y == "13" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "14" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Stand<BR>");
}
else if(Y == "14" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "15" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Stand<BR>");
}
else if(Y == "15" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "16" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Stand<BR>");
}
else if(Y == "16" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "17" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Stand<BR>");
}
else if(Y == "A2" && (D == "2" || D == "3" || D == "4" ||  D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A2" && (D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A3" && (D == "2" || D == "3" || D == "4" ||  D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A3" && (D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A4" && (D == "2" || D == "3" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A4" && (D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A5" && (D == "2" || D == "3" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A5" && (D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A6" && (D == "2" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A6" && (D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A7" && (D == "2" || D == "7" || D == "8"))
{
document.write("Stand<BR>");
}
else if(Y == "A7" && (D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A7" && (D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A8" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Stand<BR>");
}
else if(Y == "A9" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Stand<BR>");
}
else if(Y == "22" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7"))
{
document.write("Split<BR>");
}
else if(Y == "22" && (D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "33" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7"))
{
document.write("Split<BR>");
}
else if(Y == "33" && (D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "44" && (D == "2" || D == "3" || D == "4" ||  D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "44" && (D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "55" && (D == "2" || D =="3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9"))
{
document.write("Double<BR>");
}
else if(Y == "55" && (D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "66" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Split<BR>");
}
else if(Y == "66" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "77" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7"))
{
document.write("Split<BR>");
}
else if(Y == "77" && (D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "88" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Split<BR>");
}
else if(Y == "99" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "8" || D == "9"))
{
document.write("Split<BR>");
}
else if(Y == "99" && (D == "7" || D == "10" || D =="A"))
{
document.write("Stand<BR>");
}
else if(Y == "1010" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Stand<BR>");
}
else if(Y == "AA" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Split<BR>");
}
else{
document.write("This hand does not exist.");
}

//-->
</script>

</body>
</html>
```

I removed the parsefloat function and now it reads the Aces. My original problem was that I couldn't figure out how it could read multiple digits. When it reads 10, it only looks that first digit "1". Sorry, if these are dumb questions. I'm just having a hard time finding this stuff on google. I'm probably going to invest in a javascript book.

```<html>
<head>
<title>Blackjack Strategy Trainer</title>
</head>
<body>
<script type="text/Javascript">
<!--

var linebreak = "<br/>"

var Y = prompt("Enter your hand","");

var D = prompt("Enter the dealer's hand", "");

if (Y <= "8")
{
document.write("Hit<BR>");
}
else if(Y == "9" && (D == "2" || D == "7" || D == "8" || D =="9" || D == "10" || D == "A"))
{

document.write("Hit<BR>");
}
else if(Y == "9" && (D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}

else if(Y == "10" && (D == "2" || D =="3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9"))
{

document.write("Double<BR>");
}
else if(Y == "10" && (D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "11" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10"))
{
document.write("Double<BR>");
}
else if(Y == "11" && D == "A")
{
document.write("Hit<BR>");
}

else if(Y == "12" && (D == "2" || D == "3" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "12" && (D == "4" || D =="5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "13" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Stand<BR>");
}
else if(Y == "13" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "14" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Stand<BR>");
}
else if(Y == "14" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "15" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Stand<BR>");
}
else if(Y == "15" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "16" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Stand<BR>");
}
else if(Y == "16" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "17" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Stand<BR>");
}
else if(Y == "A2" && (D == "2" || D == "3" || D == "4" ||  D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A2" && (D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A3" && (D == "2" || D == "3" || D == "4" ||  D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A3" && (D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A4" && (D == "2" || D == "3" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A4" && (D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A5" && (D == "2" || D == "3" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A5" && (D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A6" && (D == "2" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A6" && (D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A7" && (D == "2" || D == "7" || D == "8"))
{
document.write("Stand<BR>");
}
else if(Y == "A7" && (D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "A7" && (D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "A8" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Stand<BR>");
}
else if(Y == "A9" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Stand<BR>");
}
else if(Y == "22" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7"))
{
document.write("Split<BR>");
}
else if(Y == "22" && (D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "33" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7"))
{
document.write("Split<BR>");
}
else if(Y == "33" && (D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "44" && (D == "2" || D == "3" || D == "4" ||  D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "44" && (D == "5" || D == "6"))
{
document.write("Double<BR>");
}
else if(Y == "55" && (D == "2" || D =="3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9"))
{
document.write("Double<BR>");
}
else if(Y == "55" && (D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "66" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6"))
{
document.write("Split<BR>");
}
else if(Y == "66" && (D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "77" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7"))
{
document.write("Split<BR>");
}
else if(Y == "77" && (D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Hit<BR>");
}
else if(Y == "88" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Split<BR>");
}
else if(Y == "99" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "8" || D == "9"))
{
document.write("Split<BR>");
}
else if(Y == "99" && (D == "7" || D == "10" || D =="A"))
{
document.write("Stand<BR>");
}
else if(Y == "1010" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Stand<BR>");
}
else if(Y == "AA" && (D == "2" || D == "3" || D == "4" || D == "5" || D == "6" || D == "7" || D == "8" || D == "9" || D == "10" || D == "A"))
{
document.write("Split<BR>");
}
else{
document.write("This hand does not exist.");
}

//-->
</script>

</body>
</html>

```

I removed the parsefloat function and now it reads the Aces. My original problem was that I couldn't figure out how it could read multiple digits. When it reads 10, it only looks that first digit "1". Sorry, if these are dumb questions. I'm just having a hard time finding this stuff on google. I'm probably going to invest in a javascript book.

This post has been edited by Dormilich: 25 June 2012 - 06:44 AM
Reason for edit:: please use [code] [/code] tags when posting code

Was This Post Helpful? 0

### #8 baavgai

• Dreaming Coder

Reputation: 6608
• Posts: 13,948
• Joined: 16-October 07

## Re: Problem with Letters and Numbers in Blackjack Strategy Program

Posted 25 June 2012 - 05:39 AM

Interesting:
```} else if(Y == "9" && (D == "2" || D == "7" || D == "8" || D =="9" || D == "10" || D == "A")) {
document.write("Hit<BR>");
} else if(Y == "9" && (D == "3" || D == "4" || D == "5" || D == "6")) {
document.write("Double<BR>");
}

```

I you're really interested in boolean logic, you can write the above as:
```} else if(Y == "9") {
if (D == "3" || D == "4" || D == "5" || D == "6") {
document.write("Double<BR>");
} else {
document.write("Hit<BR>");
}
}

```

You can apply the same to all of statements, that way you don't loose any.

Then again, what the hell kind of hand is "9"? Those aren't hands, they're cards. This is why I recommended a drop down. A hand is two cards to start and you need to consider both. Trying to parse a string from a prompt is considerably harder than taking a clean value from a selection.

Make a function. Avoid document.write. Start off with something like:
```function getStrategy(playerCard1, playerCard2, dealerCard) {
if (playerCard1=='A') {
if (playerCard2=='A') {
return "Split";
} else if (playerCard2=='2') {
return 	(dealerCard=='5'||dealerCard=='6') ? "Double" : "Hit";
} else //...

```

This post has been edited by baavgai: 25 June 2012 - 05:40 AM

Was This Post Helpful? 2

### #9 USC2587

• New D.I.C Head

Reputation: 1
• Posts: 29
• Joined: 04-June 12

## Re: Problem with Letters and Numbers in Blackjack Strategy Program

Posted 25 June 2012 - 09:56 PM

baavgai, on 25 June 2012 - 05:39 AM, said:

Interesting:
```} else if(Y == "9" && (D == "2" || D == "7" || D == "8" || D =="9" || D == "10" || D == "A")) {
document.write("Hit<BR>");
} else if(Y == "9" && (D == "3" || D == "4" || D == "5" || D == "6")) {
document.write("Double<BR>");
}

```

I you're really interested in boolean logic, you can write the above as:
```} else if(Y == "9") {
if (D == "3" || D == "4" || D == "5" || D == "6") {
document.write("Double<BR>");
} else {
document.write("Hit<BR>");
}
}

```

You can apply the same to all of statements, that way you don't loose any.

Then again, what the hell kind of hand is "9"? Those aren't hands, they're cards. This is why I recommended a drop down. A hand is two cards to start and you need to consider both. Trying to parse a string from a prompt is considerably harder than taking a clean value from a selection.

Make a function. Avoid document.write. Start off with something like:
```function getStrategy(playerCard1, playerCard2, dealerCard) {
if (playerCard1=='A') {
if (playerCard2=='A') {
return "Split";
} else if (playerCard2=='2') {
return 	(dealerCard=='5'||dealerCard=='6') ? "Double" : "Hit";
} else //...

```

I guess you could do it that way. Although, I'm not sure if it is necessary to enter 2 cards.

I say this because... You are dealt 2 cards. Then, you add up your cards. So, you couldn't be dealt only a "9" card. You can have a total of 9, such as a "4" and "5" card. When the strategy chart is referring to numbers 5-17, it is referring to the sum of the 2 cards you were dealt.

Your hand could also be A,2 and A,3... These are equivalent to 2,A and 3,A...Then you have 2,2 and 3,3... I guess commas are used in these cases to show they are not sum totals of 2 cards.

Then, when the chart refers to the dealer, it is just talking about 1 card. When the cards are dealt to the player, the player can only see 1 card from the dealer. The other card is faced down. I guess that's why the chart says "upcard".

I'm not sure if this helps. I couldn't run the code you posted earlier. I tried playing around with it. I'm still not clear on basics like variables, functions, etc. My first tutorial didn't cover topics in great detail. Today, I've been trying to learn on a site called Codecademy.com. I guess I'll have to come back to this Blackjack program when I've brushed up on the basics. I appreciate your help immensely. This is an awesome site.
Was This Post Helpful? 0

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}