Smilesprower Posted November 13, 2015 Share Posted November 13, 2015 Can someone please tell me why this doesn't work in javascript.I come from a c++ background so please excuse my ignorence.///////////////////////////////////////////////////////////////////// var mapArray = [ [0, 0, 0, 0 ,0], [0, 1, 0, 0 ,0], [0, 0, 0, 0 ,0], [0, 0, 0, 0 ,0], [0, 0, 1, 1 ,0]];/////////////////////////////////////////////////////////// for (var y = 0; y < 5; y++) { for (var x = 0; x < 5; x++) { if (mapArray[y][x] == 1) {// Do Something } } } Quote Link to comment Share on other sites More sharing options...
rich Posted November 14, 2015 Share Posted November 14, 2015 There's nothing wrong the code, it works fine for me. You probably want a strict comparison (=== 1) rather than the loose one you have there, but it still works even with that. Quote Link to comment Share on other sites More sharing options...
thatguy64 Posted November 14, 2015 Share Posted November 14, 2015 should it bemapArray[x][y]instead ofmapArray[y][x] Quote Link to comment Share on other sites More sharing options...
thatguy64 Posted November 14, 2015 Share Posted November 14, 2015 also try putting it in a beautifier Quote Link to comment Share on other sites More sharing options...
chg Posted November 14, 2015 Share Posted November 14, 2015 should it bemapArray[x][y]instead ofmapArray[y][x] also try putting it in a beautifierI don't understand your suggestions, care to explain your thinking? Can someone please tell me why this doesn't work in javascript.I come from a c++ background so please excuse my ignorence.BTW, code works for me even with strict mode. As a c++ person I'd expect more then just that the code doesn't work, ie. what are you seeing? what errors are you seeing on the console? are you using strict mode? Have you tried debugging (if not; why not ); and if so what do you see stepping through the execution? I took the liberty of trying JSLint on your code... it had some kind of lame complaints, the most meaningful of which was pointing out that your placement of "var" in the for loops may suggest a misunderstanding of Javascript scope (ie. that it lacks block scope) of course this has little bearing on the code working as intended in this case (and the placement of "var" is syntactically valid) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.