PDA

View Full Version : need help incrementing a variable with a for loop


jasrasr
06-18-2009, 02:29 PM
I am trying to increase the variable by one each time the loop is printed.

<code>

function horsetable(hid1,hid2,hid3,hid4)

{
document.write("<table border=1>");

document.write ("<tr>"); for (var x=0;x<5;x++) { document.write ("<td>"+hid1[x]+"</td>"); } document.write ("</tr>");
document.write ("<tr>"); for (var x=0;x<5;x++) { document.write ("<td>"+hid2[x]+"</td>"); } document.write ("</tr>");
document.write ("<tr>"); for (var x=0;x<5;x++) { document.write ("<td>"+hid3[x]+"</td>"); } document.write ("</tr>");
document.write ("<tr>"); for (var x=0;x<5;x++) { document.write ("<td>"+hid4[x]+"</td>"); } document.write ("</tr>");

document.write ("</table>");
}

horsetable(id22511,id21111,id21112,id21131);
</code>

This only show 4, but I have 18 for this particular set.
I think I can combine all that into a for loop as such

<code>
for (var i=0,i<4;i++)
{
document.write ("<tr>");

for (var x=0;x<5;x++)
{
document.write ("<td>"+hid[i][x]+"</td>");
}

document.write ("</tr>");
}

</code>

Also is there an easier way to print this out without all the document.write's?

unclhos
06-19-2009, 05:59 PM
I would use a while loop:

function horsetable(hid1,hid2,hid3,hid4)

{
document.write("<table border=1>");

document.write ("<tr>"); var x=0; while (x<5) { document.write ("<td>"+hid1[x]+"</td>"); x++; } document.write ("</tr>");
document.write ("<tr>"); var x=0; while (x<5) { document.write ("<td>"+hid2[x]+"</td>"); x++; } document.write ("</tr>");
document.write ("</table>");
}

var i=0;
while (i<4)
{
document.write ("<tr>");
var x=0
while (x<5)
{
document.write ("<td>"+hid[i][x]+"</td>");
x++;
}

document.write ("</tr>");
i++;
}

jasrasr
06-22-2009, 04:19 PM
That does work. I forgot to add my vars
here's a small list.
<code>
var id21111=new Array("Matrix","Armani FC","Magnolyah","15.2","$$$");
var id21112=new Array("Unregistered","Armani FC","Lots n Lots of Fame","15.2","$$$");
var id21211=new Array("Fascination PA","Armani FC","Veranda","15.3","$$$");
var id21212=new Array("Whiskey Moon","Bucharest V","Aerial Moondanse","14.3","$$$");
var id21311=new Array("Khabernet","Versace","Khabaret","?","$$$");
var id21221=new Array("Elite Design","Armani FC","Allioness","15","$$$");
var id21231=new Array("Spartacus PA","Armani FC","Autumn Sihouette","15","$$$");
var id21131=new Array("High Frequency","Armani FC","Allioness","15.1","$$$");
var id21241=new Array("Grey Hawk","Armani FC","Tennacite","15","$$$");
var id21131=new Array("High Frequency","Armani FC","Allioness","15.1","$$$");
var id22511=new Array("Wild Fire PE","Afire Bey V","GTF Judy, Judy, Judy","?","$$$");
var id22411=new Array("Pandamonium","Armani FC","Mimone (DHH)","?","$$$");
var id22441=new Array("The Elegant Edition","Jonker (DHH)","Autumn Elegance","?","$$$");
var id22331=new Array("Pizzazz PE","Picazso","Painted Lady","?","$$$");
var id22231=new Array("Show Me the Cash","Bucharest V","Moonshyne","?","$$$");
var id22111=new Array("Alacazam","Jonker (DHH)","Aerial Moondanse","?","$$$");
var id22311=new Array("Mandrini","Armani FC","Sendrini (DHH)","?","$$$");
var id21441=new Array("Maverick PA","Masquerade PA","Versiara","?","$$$");
var id21141=new Array("Silver Dollar","Versace","Taffettaa","?","$$$");

</code>

jasrasr
06-22-2009, 09:16 PM
I would use a while loop:

function horsetable(hid1,hid2,hid3,hid4)

{
document.write("<table border=1>");

document.write ("<tr>"); var x=0; while (x<5) { document.write ("<td>"+hid1[x]+"</td>"); x++; } document.write ("</tr>");
document.write ("<tr>"); var x=0; while (x<5) { document.write ("<td>"+hid2[x]+"</td>"); x++; } document.write ("</tr>");
document.write ("</table>");
}

var i=0;
while (i<4)
{
document.write ("<tr>");
var x=0
while (x<5)
{
document.write ("<td>"+hid[i][x]+"</td>");
x++;
}

document.write ("</tr>");
i++;
}


this doesn't print anything for me?