javascript - Why am I getting value increase while recalculating an array? -


i need recalculate vales of array in javascript , trying this:

        (var = 0; < totalnum; i++) {           x = 3;           if (chane[i]<rand) {             chane[i+1] += chane[i];             chane[i] = 0;           } else{             chane[i] -= x;             chane[i+1] += x;           }; 

chane array comes in values [15,4,4,0], , comes out [12,40,40,3]. totalnum 4.

i have been staring @ code trying find newbie mistake hides... new javascript, may syntax error.

you forgot convert value integer, , should pass string value loop check code:

    (var = 0; < totalnum; i++) {       // convert int mul 1       chane[i]*=1;       chane[i+1]*=1;        x = 3;       if (chane[i]<rand) {         chane[i+1] += chane[i];         chane[i] = 0;       } else{         chane[i] -= x;         chane[i+1] += x;       };     } 

if set chane = [15,4,4,0] , set totalnum = 4, result is: [12, 4, 4, 0, nan] , nan check last value because when i=3 chane[3] equal 0 , chane[i+1] equal undefined , undefined multiply every number nan, can fix change upper bound (in example:set totalnum 3, fixed changing totalnum = chane.length-1) or add if


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -