CA1K Productions CA1K Productions - 6 months ago 9
jQuery Question

JS/JQuery global array error in certain functions

I am having an issue with my BlackJack script. The problem rests in one of the arrays. The unresponsive array is 1 of 2 arrays that are carrying data to the same function.

Here are the arrays (

dealerCards
works):

// CREATE DATABASE ARRAYS FOR THE DEALER AND PLAYER HANDS
var dealerCards = new Array(); //the dealers hand...
var playerCards = new Array(); //the players hand...
var playerCardsSplit = new Array(); //this is an appended second player hand via split function


The troublesome function:

function splitPrint()
{
document.open();
document.clear();
document.write("</kbd></font>");
//start the table for the card cell loops
document.write("<BODY bgcolor=green>");
document.write("<table cellspacing=10><tr id=\"tr1\"><tr id=\"tr2\"><tr id=\"tr3\"><tr id=\"tr4\"></table>");
//***print dealer cards
//NOTE THAT THE DEALER ROW WILL NOT SPLIT, ONLY YOU ARE SPLITTING YOUR HAND
//start the first card table cell
$("#tr1").append("<TD id=\"startcard\" style=\"border-radius:5px; box-shadow: 2px 2px 2px #111111;\" BGCOLOR=#AC0000 ALIGN=CENTER WIDTH=150 HEIGHT=225>");
$("#startcard").append("<font style=\"font-family:sans-serif;color:white;font-size:32px;\">CA1K</font>");
$("#tr1").append("</TD>");
//start the table cell
$("#tr1").append("<TD id='dtd' style=\"border-radius:5px; box-shadow: 2px 2px 2px #111111;\" BGCOLOR=WHITE ALIGN=CENTER WIDTH=150 HEIGHT=225>");
//check the card color to set the font
if (dealerCards[1][2] == "♥"){$("#dtd").append("<FONT style=\"color:red;\" size=7>");}else{
if (dealerCards[1][2] == "♦"){$("#dtd").append("<FONT style=\"color:red;\" size=7>");}
else{$("#dtd").append("<FONT style=\"color:black;font-size:32px;\" size=7>");}}
//writing dealer cards...
$("#dtd").append(dealerCards[1][0] + " " + dealerCards[1][2]);
$("#dtd").append("<BR><FONT SIZE=2 color=black>");
$("#dtd").append(dealerCards[1][0] + " " + dealerCards[1][1]);
$("#dtd").append("<BR><FONT SIZE=3 color=black>");
$("#dtd").append("<P>");
$("#dtd").append("<kbd>Value is " + dealerCards[1][3]);
$("#dtd").append("<P>");
$("#dtd").append("<kbd>for Dealer");
$("#tr1").append("</TD>");
//***end dealer cards
$("#tr2").append("<TD colspan=2 align=center><FONT color=white><kbd style=\"font-family:sans-serif;text-shadow: 2px 2px 2px #111111;\">Player = " + playerAcesTotal + "</TD>");
$("#tr2").append("<TD align=center WIDTH=150><button onclick=\"\hitMe\(\)\">Hit</button></TD>");
$("#tr2").append("<TD><button onclick=\"stand\(\)\">Stand</button></TD>");
$("#tr2").append("<TD><button onclick=\"split\(\)\">Split</button></TD>");
//start the table cell
$("#tr3").append("<TD id='ptd1' style=\"border-radius:5px; box-shadow: 2px 2px 2px #111111;\" BGCOLOR=WHITE ALIGN=CENTER WIDTH=150 HEIGHT=225>");
//check the card color to set the font
if (playerCards[0][2] == "♥"){$("#ptd1").append("<FONT style=\"color:red;\" size=7>");}else{
if (playerCards[0][2] == "♦"){$("#ptd1").append("<FONT style=\"color:red;\" size=7>");}
else{$("#ptd1").append("<FONT style=\"color:black;\" size=7>");}}
//print player cards
//the first card...
$("#ptd1").append(playerCards[0][0] + " " + playerCards[0][2]);
$("#ptd1").append("<BR><FONT SIZE=2 color=black>");
$("#ptd1").append(playerCards[0][0] + " " + playerCards[0][1]);
$("#ptd1").append("<BR><FONT SIZE=3 color=black>");
$("#ptd1").append("<P>");
$("#ptd1").append("<kbd>Value is</kbd> " + playerCards[0][3]);
$("#ptd1").append("<P>");
$("#ptd1").append("<kbd>for Player</kbd>");
$("#tr3").append("</TD>");
//the second row...
playerCards[1] = playerCardsSplit[0]; //now we are making this card in a hand independent from the original
$("#tr4").append("<TD id='std1' style=\"border-radius:5px; box-shadow: 2px 2px 2px #111111;\" BGCOLOR=WHITE ALIGN=CENTER WIDTH=150 HEIGHT=225>");
if (playerCardsSplit[0][2] == "♥"){$("#std1").append("<FONT style=\"color:red;font-size:32px;\">");}else{
if (playerCardsSplit[0][2] == "♦"){$("#std1").append("<FONT style=\"color:red;font-size:32px;\">");}
else{$("#tr4").append("<FONT style=\"color:black;font-size:32px;\">");}}
$("#std1").append(playerCardsSplit[0][0] + " " + playerCardsSplit[0][2]);
$("#std1").append("<BR><FONT SIZE=2 color=black>");
$("#std1").append(playerCardsSplit[0][0] + " " + playerCardsSplit[0][1]);
$("#std1").append("<BR><FONT SIZE=3 color=black>");
$("#std1").append("<P>");
$("#std1").append("<kbd>Value is</kbd> " + playerCardsSplit[0][3]);
$("#std1").append("<P>");
$("#std1").append("<kbd>for Player</kbd>");
$("#tr4").append("</TD>");
//**end of player cards loop
}


Whenever I call the array object
playerCards[1]
, the function interprets it as
undefined
.
What is also confusing about this is, is that the other functions interpret the
playerCards[1]
just fine.

Help with this would be much appreciated.

Sincerely, CA1K.

EDIT: What I also notice about the function is, is that it is also not printing the formats provided by the if statements within the function.

Answer

First you say:

var playerCardsSplit = new Array();

Then you say:

playerCards[1] = playerCardsSplit[0];

There is no value at the 0th index of playerCardsSplit.

Comments