Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Baffled by something so simple

    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.

    Code:
    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]);
            }
            i++;
        }
        alert(totalCredits);
        $("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
    Code:
    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.

  • #2
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •