0

So, I've got a method to grab particular DIV from x, y coordinates:

this.getCell = function (x, y){
      this.index = x + y * this.width;
      return this.cells[this.index];
}

I want to use my method with another one:

this.computeCellNextState = function(x, y){

      var nearbies = ['x-1,y-1','x,y-1','x+1,y-1'];
      var splitter = nearbies[0].split(',');

      console.log(this.getCell(splitter[0],splitter[1])); // returns undefined

}

What I want to achieve:

this.getCell(x-1,y-1)

x-1,y-1 are from nearbies[0]

I want to split one string of 'nearbies' and use as 2 parameters.

1 Answer 1

1

'x-1,y-1' etc are just strings, they don't mean anything for getCell. You have to work with actual expressions in computeNextState, e.g.

this.computeCellNextState = function(x, y){

      var nearbies = [[x-1,y-1],[x,y-1],[x+1,y-1]];

      console.log(this.getCell(...nearbies[0]))

}
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I missed, rookie mistake. 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.