javascript - parseInt() returns NaN instead of integer -
i'm trying implement route-finder using javascript. so, create grid 2d array, , insert 'x' (walls), 'o' (open space), '0' (start), , 'g' (goal). increment open squares start goal, end looks this: '0',1,2,3,4,5 ... g
however, grid never goes past 0 , 1. console keeps returning nan remaining open spaces , suspect it's typeerror using parseint(). please see code below - appreciate help.
<!doctype html> <head> <link rel='stylesheet' type='text/css' href = 'routes.css'> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body></body> <script> //o open space x wall var = [['0','o','x','x'], ['x','o','o','o'], ['x','o','x','o'], ['x','o','o','g']]; /*printed grid looks var = [[0,1,'x','x'], ['x',nan,nan,nan], ['x',nan,'x',nan], ['x',nan,nan,'g']];*/ function move(startr,startc){ //(0,0) startr,startc var north = startr-1; var south = startr+1; var east = startc+1; var west = startc-1; //discard out of bounds locations if (north<0){north=startr;} if (south>3){south=startr;} if (east>3){east=startc;} if (west<0){west=startc;} if (a[north][startc]=='o'){newlocation(north,startc,startr,startc);} if (a[south][startc]=='o'){newlocation(south,startc,startr,startc);} if (a[startr][west]=='o'){newlocation(startr,west,startr,startc);} if (a[startr][east]=='o'){newlocation(startr,east,startr,startc);} } //newlocation() increments square moved , pass new location function 'move ' function newlocation(northsouth,eastwest,origr,origc){ var origvalue = parseint(a[origr,origc]); console.log(origvalue); //prints nan console var newvalue = ++origvalue; a[northsouth][eastwest]=newvalue; console.log(a[origr,origc]); return move(northsouth,eastwest); } move(0,0); //print board screen (r=0;r<4;r++){ document.write('<br>'); (c=0;c<4;c++){ var l = document.createelement('div'); var t = document.createtextnode(a[r][c]); $(l).addclass('letterboxes'); l.appendchild(t); document.body.appendchild(l); // document.body.appendchild(letter); } } </script> </html>
Comments
Post a Comment