Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
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? – lpd Feb 4 '12 at 0:28
where are you declaring roombuffer? – hrishikeshp19 Feb 4 '12 at 0:31
Thanks everyone for your answers! I should have checked the javascript console before asking here though – John Doe Feb 4 '12 at 1:47

4 Answers

up vote 4 down vote accepted

change

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

to

for (i = 0; i < roomWidth / tileWidth; i += 1) {
share|improve this answer

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");
share|improve this answer
You're misunderstanding javascript's scoping rules. i and j will not be discarded after the loops. – david Feb 4 '12 at 0:33
sorry. fixed my answer. – TestSubject528491 Feb 4 '12 at 0:43
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 Feb 4 '12 at 0:46
@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... – TestSubject528491 Feb 4 '12 at 1:01
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. – david Feb 4 '12 at 1:38

Have a look at this fiddle.

change your

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

to

for (i = 0; i < roomWidth / tileWidth; i += 1)
share|improve this answer

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))    
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.