PDA

View Full Version : Nested If Then Else Statements


areyouhappynow
10-09-2005, 09:34 AM
This code uses a nested IF statement to put a pack of cards into an array of records.
But for some reason the cards arn't going in.. like.. at all?
I've put a document.write piece of code at end to display the card in the first slot and [object Object] appears?


<HTML>
<TITLE>DAT Card Game</TITLE>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

var Cards = new Array (52)
var Shuffled = new Array (52)

function Card(suit,face)
{this.suit=suit;
this.face=face;
}

function Stack(suit,face)
{
var suit
var face
index=0
count=1
for (count=1;count<=52;count++)
if (count<=39)
{if (count<=13)
{suit='hearts'
face=count
Cards[index]=new Card(suit,face)
index++
count++}
else
{if (count<=26)
{suit='spades'
face=count
Cards[index]=new Card(suit,face)
index++
count++}

else
{if (count<=39)
{suit='diamonds'
face=count
Cards[index]=new Card(suit,face)
index++
count++}}}}
else
{suit='clubs'
face='count'
Cards[index]=new Card(suit,face)
index++
count++}

document.write(Cards[0])
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="MyForm">
<INPUT TYPE="BUTTON" VALUE="stack" onClick="Stack()">
</FORM>
</BODY>
</HTML>


If you copy and paste into notepad and then look at it in your browser you'd see what I mean.
Any ideas?

EDIT: I think my syntax for creating An Array Records is totally wrong just looking through tutorials. Each object in the array has to have a name?

EDIT 2: Ignore this post. I've given up on the Object in Array structure.

pccode
10-09-2005, 12:23 PM
I've edited your script and removed some unnecessary code. The face and suit of each card are saved in a multi-dimensional array named Cards. To access each variable use the following syntax:

Cards[1][0] //suit
Cards[1][1] //face
Cards[2][0] //suit
Cards[2][1] //face

and so on. You will notice that after it prints all of the variables in the array, a comma appears at the beginning of the other variables. That is because there is no Cards[0] variables. The Cards array begins at [1].


<HTML>
<TITLE>DAT Card Game</TITLE>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function Stack() {
var Cards = new Array(52);
for (i = 1; i < Cards.length; ++i) {
Cards[i] = new Array(2);
}
var suit;
var face;
for (count=1;count<=52;++count) {
if (count<=39) {
if (count<=13) {
suit='hearts';
face=count;
temp = suit + ":" + face;
Cards[count]=temp.split(":");
} else {
if (count<=26) {
suit='spades';
face=count;
temp = suit + ":" + face;
Cards[count]=temp.split(":");
} else {
suit='diamonds';
face=count;
temp = suit + ":" + face;
Cards[count]=temp.split(":");
}
}
} else {
suit='clubs';
face=count;
temp = suit + ":" + face;
Cards[count]=temp.split(":");
}
}
//the following code is only used to test the script
var check = Cards;
document.getElementById('test').innerHTML = check;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="MyForm">
<INPUT TYPE="BUTTON" VALUE="stack" onClick="Stack()">
</FORM>
<br><br>
<span id="test">
Output will appear here.
</span>
</BODY>
</HTML>

areyouhappynow
10-10-2005, 06:56 AM
Thanks alot!