MrScreeps
MrScreeps

Reputation: 21

Why isn’t this array access working?

Why doesn’t this method access work? No matter what we write into the method, the click never executes what we want.

<html>

<head>
  <script language="JavaScript">
    var Spiel1 = new TicTacToe();

    function TicTacToe() {
      var spielfeld = new Array[3][3];
      var zaehler = 0;
      this.Setzen = function(x, y) {
        document.getElementById("r0p0").innerHTML = "X";
      }
    }
  </script>
</head>

<body>
  <table>
    <tr>
      <td id="r0p0" onclick="Spiel1.Setzen(0,0)"></td>
    </tr>
  </table>
</body>

</html>

Upvotes: 1

Views: 53

Answers (2)

Fanyo SILIADIN
Fanyo SILIADIN

Reputation: 792

just do :

spielfeld = new Array(3);
for (var i = 0 ; i< 3; i++){
 spielfeld[i] =  new Array(3);
}

or just

spielfeld = [[,,,],[,,,],[,,,]];

note that there is 3 commas because this array [,,] only has 2 elements!!

because that doesn't really matter..

Upvotes: 1

zhm
zhm

Reputation: 3641

There is error in var spielfeld = new Array[3][3]; If you remove this line, your code will work.

How to define multidimensional array?

var spielfeld = new Array();
spielfeld[0] = new Array();
spielfeld[1] = new Array();

Another way, if you want to initialize array when declaring:

var a = new Array([1,2,3], [4,5,6], [7,8,9]);
var b = [[1,2,3], [4,5,6], [7,8,9]];

Upvotes: 2

Related Questions