Dabbous
Dabbous

Reputation: 167

JS sequence count using web service array

    /*create a table with webservice provided objects ex Records*/
    function createTable(table, webSerArr, selectName){
        /*trying to find a way to make this more global -- not there yet*/

        /*for loop to add the rows*/
        for(var i = 0; i < webSerArr.length; i++){
            row = table.insertRow(i+1);

            /*The following creates cells and fills them with the required information*/
            row.insertCell(0).innerHTML =  (webSerArr.length-i) ;
            row.insertCell(1).innerHTML = webSerArr[i].clientno ;
            row.insertCell(2).innerHTML = webSerArr[i].clientname ;
            row.insertCell(3).innerHTML = createrow(table, webSerArr, selectName,i);
            row.insertCell(4).innerHTML = webSerArr[i].admdate ;
}


function createrow(table, webSerArr, selectName,i){
     var count=0;
     if( i==0 )
        return 1;
    else if( webSerArr[i-1].admdate!= webSerArr[i].admdate  ){ 
        return 1;
    }
    else if(webSerArr[i-1].admdate== webSerArr[i].admdate && count==0  ){
        count=count+1;
        return 1;
    }
    else if(  webSerArr[i-1].admdate== webSerArr[i].admdate && count==1){
        count=count+1;
        return 2;
    }
    else if( webSerArr[i-1].admdate!= webSerArr[i].admdate && count==2)
        return 3;
    }

function creattable(...) creates rows and fills them with values from webservice. the problem is in filling this row : "row.insertCell(3).innerHTML" I created a function "createrow(...)", as you see above, however the problem is that in each time we are calling this function the value of count will return to 0 and it is not updated. How is it possible to make count be updated, as required, after each call.

Note: (webSerArr[i].admdate: returns the admission date. On that base we are filling the values).

Upvotes: 0

Views: 52

Answers (1)

Erazihel
Erazihel

Reputation: 7605

The problem is that you init the count variable every time the createrow function is called. Just initialize it outside of the function:

var count = 0;

function createrow(table, webSerArr, selectName,i){
     if( i==0 )
        return 1;
    else if( webSerArr[i-1].admdate!= webSerArr[i].admdate  ){ 
        return 1;
    }
    else if(webSerArr[i-1].admdate== webSerArr[i].admdate && count==0  ){
        count=count+1;
        return 1;
    }
    else if(  webSerArr[i-1].admdate== webSerArr[i].admdate && count==1){
        count=count+1;
        return 2;
    }
    else if( webSerArr[i-1].admdate!= webSerArr[i].admdate && count==2)
        return 3;
    }
}

Upvotes: 1

Related Questions