Effect of mixing commas and semicolons in javascript variable declaration -


in code below, horiz being declared , run through loop fills empty arrays. same verti on second line etc. "var" declaration apply horiz (i.e semicolon breaks "var" declaration) or "var" declaration initialize horiz, verti, here, path , unvisited?

var horiz =[]; (var j= 0; j<x+1; j++) horiz[j]= [],     verti =[]; (var j= 0; j<x+1; j++) verti[j]= [],     here = [math.floor(math.random()*x), math.floor(math.random()*y)],     path = [here],     unvisited = []; 

edit: added full function here clarity. variables in function not global or being initialized outside function.

edit 2: code here: http://rosettacode.org/wiki/maze_generation#javascript

function maze(x,y) {     var n=x*y-1;     if (n<0) {alert("illegal maze dimensions");return;}     var horiz =[]; (var j= 0; j<x+1; j++) horiz[j]= [],         verti =[]; (var j= 0; j<x+1; j++) verti[j]= [],         here = [math.floor(math.random()*x), math.floor(math.random()*y)],         path = [here],         unvisited = [];     (var j = 0; j<x+2; j++) {         unvisited[j] = [];         (var k= 0; k<y+1; k++)             unvisited[j].push(j>0 && j<x+1 && k>0 && (j != here[0]+1 || k !=     here[1]+1));     }     while (0<n) {         var potential = [[here[0]+1, here[1]], [here[0],here[1]+1],             [here[0]-1, here[1]], [here[0],here[1]-1]];         var neighbors = [];         (var j = 0; j < 4; j++)             if (unvisited[potential[j][0]+1][potential[j][1]+1])                 neighbors.push(potential[j]);         if (neighbors.length) {             n = n-1;             next= neighbors[math.floor(math.random()*neighbors.length)];             unvisited[next[0]+1][next[1]+1]= false;             if (next[0] == here[0])                 horiz[next[0]][(next[1]+here[1]-1)/2]= true;             else                  verti[(next[0]+here[0]-1)/2][next[1]]= true;             path.push(here = next);         } else              here = path.pop();     }     return {x: x, y: y, horiz: horiz, verti: verti}; } 

var horiz =[]; //variable declaration  (var j= 0; j<x+1; j++) //for loop start   horiz[j]= [], verti =[]; //variable declaration  //for loop end   (var j= 0; j<x+1; j++) //for loop start     verti[j]= [], here = [math.floor(math.random()*x), math.floor(math.random()*y)], path = [here], unvisited = []; //variable declaration //for loop end 

you're getting confused due missing braces

var horiz =[]; //variable declaration  (var j= 0; j<x+1; j++) {   horiz[j]= [], verti =[]; //variable declaration }   (var j= 0; j<x+1; j++) {     verti[j]= [], here = [math.floor(math.random()*x), math.floor(math.random()*y)], path = [here], unvisited = []; //variable declaration } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -