homepage Welcome to WebmasterWorld Guest from 54.234.228.64
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
For Loop Problems
j2trumpet




msg:3909193
 7:16 am on May 8, 2009 (gmt 0)

I'm trying to build a list of team captains and players for a golf tournament. I've been able to get the team name and its captain's name to show up properly in a table, but I can't figure out how to add the regular players under their team name automatically. When I try to run the addPlayers function from the document.write the script breaks. Maybe that's not possible. I'm pretty new to javascript. Any help would be greatly appreciated. Thanks!

var captains = new Array();
captains[0] = 'Dave Busch,DaveB,5280534';
captains[1] = 'Gary Bushman,GaryB,5280698';
captains[2] = 'Gary Fox,GaryF,2979466';
captains[3] = 'Rick Fritzmeier,RickF,5280761';
captains[4] = 'Freddy Gallego,FreddyG,5280687';
captains[5] = 'Lee Handley,LeeH,5280723';
captains[6] = 'Ray Harrison,RayH,5280549';
captains[7] = 'Paul Kolarik,PaulK,5280606';
captains[8] = 'Gary Luiz,GaryL,5280783';
captains[9] = 'Rob Maxey,RobM,5280711';
captains[10] = 'Arthur Owings,ArthurO,5280734';
captains[11] = 'Dane Owings,DaneO,5280749';
captains[12] = 'Robert Raffaelli,RobertR,5145685';
captains[13] = 'Stephen Scholz,StephenS,5280622';
captains[14] = 'David Sirsi,DavidS,5280561';
captains[15] = 'Jesse Smith,JesseS,5145685';
captains[16] = 'Ted Smith,TedS,5280590';
captains[17] = 'Robert Tom,RobertT,5280777';
captains[18] = 'Brian Wiese,BrianW,5280574';

for (var i=0; i<captains.length; i++) {
var captain = captains[i].split(',');
var captainLast = captain[0].split(' ');

document.write ('<td><span style="font-size: 1.2em; font-weight: bold; text-decoration: underline;">' + captainLast[1] + ' Team</span><br /><a href="' + captain[2] + '">' + captain[0] + '</a>' + addPlayers() + '</td>');

if ((i==3)(i==7)(i==11)(i==15)(i==19)) {
document.write ('</tr><tr><td colspan="4">&nbsp;</td></tr><tr>');
}
}

var players = new Array();
players[0] = 'LeeH,Don Jacobs,DonJ,';

function addPlayers(thisPlayer) {
for (var j=0; j<players.length; j++) {
var player = players[i].split(',');

if (thisPlayer==player[0]) {
document.write ('<br /><a href="' + player[3] + '">' + player[1] + '</a><br />');
}
}
}

 

daveVk




msg:3909214
 8:10 am on May 8, 2009 (gmt 0)

var player = players[
i].split(',');

j ?

j2trumpet




msg:3909500
 3:06 pm on May 8, 2009 (gmt 0)

That was certainly a typo. I also realized that in the code I posted above, in the main document.write section, I forgot to write (captain[1]) into the addPlayers function call. It's still not working though. IE's error message is saying that players.length is null or not an object. Any ideas?

whoisgregg




msg:3909567
 4:09 pm on May 8, 2009 (gmt 0)

I'm pretty sure if you move the block starting with
var players through the end of the addPlayers function above the for() loop, it should work.

Added: document.write is a funny thing because it has to execute immediately, so it throws off a lot of patterns that would otherwise work if you were doing regular DOM manipulation.

Dabrowski




msg:3909656
 6:09 pm on May 8, 2009 (gmt 0)

Here's an excellent demo of how to properly add rows/cells to a table:
[w3schools.com...]

Right, try this:
if ( i==3 i==7 i==11 i==15 i==19) {

Ironically your way will work but not for the reason you may think!

(also, 'i' will never eaqual 19 at the moment as you only have 18 captains)

Also, I like to define array loops like this, it looks a little neater:
for (var i=0; captains[i]; i++) {

A FOR statement is basically 3 commands:
for( start with this; while this; do this) {

I think the error is because players is not defined before the addPlayers function is run. Your script will run in the order it is written, your captains loop is jumping to a part of the script where at that point, players is not defined. Imagine it like this:

1. Define captains
2. Loop captains
-> 3. Call addPlayers
4. Define players

You see?

If you move your players array creation immediately after where you define captains that should fix it.

j2trumpet




msg:3916726
 8:03 pm on May 19, 2009 (gmt 0)

I've used the help given here and gotten help from another designer I know, but I'm still having problems. I can now get the first player from the players array to show up on their respective team, but no more. For example, Don Jacobs will show up under Lee Handley, but if I try adding another player to the Handley team, they don't show. Help!

Here's an updated version of my code minus the arrays:

for (var i=0; i<captains.length; i++) {
var captain = captains[i].split(',');
var captainLast = captain[0].split(' ');
var captainID = captain[1];

document.write('<td><span style="font-size: 1.2em; font-weight: bold; text-decoration: underline;">' + captainLast[1] + ' Team</span><br /><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=' + captain[2] + '">' + captain[0] + '</a>' + addPlayers(captainID) + '</td>');

if ((i==3)(i==7)(i==11)(i==15)) {
document.write('</tr><tr><td colspan="4">&nbsp;</td></tr><tr>');
}
}

function addPlayers(thisPlayer) {

for (var j=0; j<players.length; j++) {
var player = players[j].split(',');

if (thisPlayer==player[0]) {
return '<br /><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=' + player[3] + '">' + player[1] + '</a>';
}

}
return ' ';
}

Dabrowski




msg:3916736
 8:18 pm on May 19, 2009 (gmt 0)

I just noticed this line:
if ((i==3)(i==7)(i==11)(i==15)) {

Can be changed to:
if( !( i %4)) {

That will give you a line break every 4 lines, right?

j2trumpet




msg:3916804
 10:19 pm on May 19, 2009 (gmt 0)

I don't know Javascript well enough to know if it will do that, but that's the idea. Close the column/row and start a new one after every four columns.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved