View Full Version : Baffled by something so simple

07-09-2011, 11:31 PM
I have a simple javascript function in a page using the DataTables JQuery plugin, but am having an issue. I have a While loop that cycles through a 2D array of table data. The intent of the loop is to grab the first column on every row of a table, and if there is a value in the column, add it to a running total. I've confirmed that the If statement only executes when there is something in the cell, so that is working fine.

function fnCalculateGPA ( ) {
var tableData = $('#myDataTable').dataTable().fnGetData();
var totalCredits = 0;
var i = 0;
while (i <= 21){
if (tableData[i][0]){
totalCredits += Number(tableData[i][0]);
$("div.gpb").html('<b>GPA:</b> <label name="gpa_display">'+totalCredits+'</label>');

When I run this as is, the function seems to stall and the alert on the bottom does not even execute. If I take the While loop out and manually loop through the array myself, everything works fine. If I comment out
totalCredits += Number(tableData[i][0]); everything is fine, minus the total is not calculated obviously. What on that line is causing the While loop to stall? What am I missing here? I know it is going to be something simple, but this has been driving me nuts for hours now!! Any help would be appreciated.

07-09-2011, 11:57 PM
NVM, I'm an idiot. It was simple. There is only 21 rows in the table, my counter started at 0, and I was looping to 21. It was getting stuck on the 22nd row which does not exist.