2

So, I need to fill a bi-dimensional array in JavaScript and I'm doing it the following way:

var i = 0, j = 0;
for (i = 0; i < roomWidth / tileWidth; i += 1) {
     roomBuffer[i] = [];
}
for (i = 0; roomWidth / tileWidth; i += 1) {
     for (j = 0; j < roomHeight / tileHeight; j += 1) {
          roomBuffer[i][j] = 1;
     }
}
alert("Hello world");

The problem is that it not only doesn't work but any code that comes after it, it's not executed. In this case the alert("Hello world");. What am I doing wrong guys?

3
  • If your script stops executing then your browser should log an error in its console. Hopefully that could give you a clue as to what's going on; it's hard for us to know without the full code, for example what is the roomWidth variable? Commented Feb 4, 2012 at 0:28
  • where are you declaring roombuffer? Commented Feb 4, 2012 at 0:31
  • Thanks everyone for your answers! I should have checked the javascript console before asking here though Commented Feb 4, 2012 at 1:47

4 Answers 4

4

change

for (i = 0; roomWidth / tileWidth; i += 1) {

to

for (i = 0; i < roomWidth / tileWidth; i += 1) {
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need to declare i and j before the loops. If their existence is solely for the loops, they are better off as local variables. Also, you can combine loops.

Also, what @Yuriy Zubarev said is right. The middle statement in the for-loop is a condition that must hold true throughout the loop.

for (var i = 0; i < roomWidth / tileWidth; i++) {
     roomBuffer[i] = [];
     for (var j = 0; j < roomHeight / tileHeight; j++) {
          roomBuffer[i][j] = 1;
     }
}
alert("Hello world");

4 Comments

You're misunderstanding javascript's scoping rules. i and j will not be discarded after the loops.
Don't call them local variables :S and it's not good to declare them inside the for loop. The general consensus is manually hoisting variable declarations to the top of the containing function is best.
@david really?? I'm new to JavaScript but I've been coding in Java for years (ok, so only 3 years) but I was always taught that it's best to declare the loop counter inside the loop definition...
That's because Java is block scoped, javascript is function scoped. If you declare them inside the loop definition you're just lying to yourself.
1

Have a look at this fiddle.

change your

for (i = 0; roomWidth / tileWidth; i += 1)

to

for (i = 0; i < roomWidth / tileWidth; i += 1)

Comments

0

You can simplify your code by using a small helper function

// create an array of length len filled with value
fill = function(len, value) {
    for (var a = []; len-- > 0; a.push(value));
    return a;
}

Your code:

size = roomWidth / tileWidth
roomBuffer = fill(size, fill(size, 1))    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.