The for loop is the really important part of my collectschedule() function, because it makes sure to scrape all the necessary data set in the HTML. Here is only my for loop.
for (var rownumber = 1; rownumber < totalrows; rownumber += 2){
var schedule = {};
var tablerows = scheduletable['children'][0];
var tablerow = tablerows.children[rownumber];
var datetime = tablerow.children[0].innerText;
var comp = tablerow.children[2].innerText;
var tshirt = tablerow.children[2].innerHTML;
var tshirtcolor = "- Home";
if (tshirt.indexOf("<") != 0){
var tshirtcolor = "- Away"
};
if (tshirtcolor == "- Away"){
comp = comp.slice(3)
};
datetime = adjustdate(datetime);
schedule['game'] = game;
schedule['datetime'] = datetime;
schedule['competitor'] = comp;
schedule['tshirtcolor'] = tshirtcolor;
schedule['teamname'] = teamname;
finalschedule.push(schedule);
game = game + 1;
};
console.log( finalschedule );
return finalschedule;
};
A for loop in javascript is set up like this: for(initialization; test; iteration){where my code goes}; Initialization is the starting point of the for loop. Here I designate my starting point as row number 1, which is also the first line the schedule table. The second part is a test, which basically asks that if this statement is true go through the loop again. Here I am stating: if the rownumber is less than the totalrows, I am to continue through the loop. This makes sure that once I’ve gone through all the rows I stop. The last part of a javascript for loop is the iteration, or an action taken each time through. Here I specify that after each processing the variable rownumber is to increase by 2. This is because the schedule table information is only included in the odd numbered rows.
Each time the for loop runs, the first thing to happen is the creation of schedule, an empty dictionary variable. This dictionary will hold all the information needed from each row. In order to reach the rest of the data, I have to ‘traverse the DOM tree’. Some websites are made up of tables inside tables. To reach information you sometimes to go inside one table, then inside a table that is listed in that table, and you keep going until you reach the area you’re looking for. Luckily I only had three hops to do, shown below:
var tablerows = scheduletable['children'][0];
var tablerow = tablerows.children[rownumber];
I went into the original table variable, called scheduletable, and created a new variable tablerows. Then I go further into tablerows and create a new variable called tablerow. As mentioned above, each tablerow includes the necessary information for each game. At this point I can easily scrape the rest of the needed data. In order I collect the datetime of the event, the comp or the team you’re competing against, and whether or not you’re home or away in the tshirtcolor section. To collect tshirtcolor I have to ‘traverse the DOM tree’ again, but that’s the last time.
Once all the data has been stored in variables I then insert it into the dictionary, after a few minor changes. First I run a function against the datetime information I collect, as I need to alter it to make it more easily adjustable in my other functions I’ll be talking about over the next week or so. I’ll explain how the adjustdate() function works tomorrow.
datetime = adjustdate(datetime);
schedule['game'] = game;
schedule['datetime'] = datetime;
schedule['competitor'] = comp;
schedule['tshirtcolor'] = tshirtcolor;
schedule['teamname'] = teamname;
finalschedule.push(schedule);
game = game + 1;
Then I assign each variable of information as a piece of my dictionary. To do that in javascript you simply follow this formula: dictionaryname[‘dictionaryvaluename’] = value to be added to dictionary. Thus when I insert schedule[‘teamname’] = teamname; I am adding a variable to my dictionary called ‘teamname’ that is made up of the same information my teamname variable had above. Once the dictionary schedule has been filled with the rows information I add it to my finalschedule list by doing this: finalschedule.push(schedule);. The last odd thing I do is increase my variable game by one. That way as I go through the scheduletable and collect each game, I’m identifying which game of the season it is accurately.
The last two things I do to this for loop is I use what is the equivalent of a python ‘print’ statement to aid with debugging. That is the: console.log(finalschedule);. Once the loop has finished running it will print the contents of my finalschedule list so that I can figure out what’s going on. Lastly I return the dictionary I’ve created. That way I can use the information I’ve collected.
console.log( finalschedule );
return finalschedule;
Up Next Time: The adjustdate() function that runs inside my collectschedule() function