Solution 1 :

This should do what you are looking for.

var ar = new Array(10)
for(i = 0; i < 10; i++) {
    ar[i] = new Array(10)
}
for(i = 0; i < 10; i++){
    for(j = 0; j< 10; j++) {
        ar[i][j] = 1
    }
}
console.log(ar)

Solution 2 :

Check if the element that you want to use exists, if not, create it

var i = 0;
var j = 0;
var arr = [];

for( i = 0; i < 8; i++) {
  for( j = 0; j < 8; j++) {
    arr[i] = arr[i] || []; // Create array if needed
    arr[i][j] = 'value';
  }
}

Problem :

I am learning to use Javascript. I tried to fill an empty matrix, through a For cycle. I think that the sentence I declare has logic, and it should work, I have something intrigued. Anyone know the reason why Javascript does not work with this code?

I am learning to use Javascript. I tried to fill an empty matrix, through a For cycle. I think that the sentence I declare has logic, and it should work, I have something intrigued. Anyone know the reason why Javascript does not work with this code?

var i = 0 , j = 0;
var arr = [[],[]];

for( i = 0; i < 8; i++){ 

  for( j = 0; j < 8; j++){

     arr[i][j] = 7;   
     alert(arr[i][j]);

  }    

   alert(arr[i][j]);

}

Comments

Comment posted by izambl

After

By