1

I'm trying to make 2D field:

var field=[];
    N = 8;
    M = 12;

for (Y=0;Y<M;Y++){

  for (X=0;X<N;X++){
    field[X,Y]=0;      //trying to make 2d field with coordinates X,Y
  }; 
};

function functiontest(){
  field[3,10]=1;      //trying to set to coordinates "3,10" value 1
  alert(field[2,10]);   //ALERTS 1 for some reason
};

I'm trying to set value 1 to coordinates "3, 10" ,but it sets value 1 to all coordinates, which have second coordinates (X,10). When I add script, that sets blue background to DIV with value 1 (id is same as coordinates), it sets it for the whole line.

function functionblue(){
  for (Y=0;Y<M;Y++){
    for (X=0;X<N;Z++){
      if (field[X,Y]==0){
        $("#z"+X+""+Y).css("background","blue");     //Divs and its IDs work properly
      }; 
    };
  };
}; 

Image Link.

so how do I set value just toto one point (not line)?

1
  • 3
    Change field[X,Y] to field[X][Y]. Commented Jan 17, 2015 at 14:37

1 Answer 1

2

Try this for a 8 × 10 field

n= 8;
m= 12;
var field1= new Array(n);

for (Y=0;Y<n;Y++){
  var field2= new Array(m);
  for (X=0;X<m;X++){
    field2[X]=0;
  }; 
  field1[Y]= field2;
};

function functiontest(){
  field1[3][10]=1;     
  alert(field1[2][10]);
};
Sign up to request clarification or add additional context in comments.

1 Comment

This was a very helpfull answer, functionblue() with the change @thefourtheye mentioned works properly now. Thank you!

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.