3

I have an array

cells = [0, 0, 0, 0, 0, 0, 0, 0, 0];

which is updated with a jQuery function

 $(document).ready(function(){
...
    $('#box').click(function(e){

        var indexArray = e.target.id.split('_');

        if (indexArray.length > 1) {
            var index = indexArray[1];

            if (cells[index] == 0){
                cells[index] = move;
...
})

I want to make a cross-check of cells array groups. for example:

(cells[0] + cells[1] + cells[2]);   // row 1
(cells[3] + cells[4] + cells[5]);   // row 2
(cells[6] + cells[7] + cells[8]);   // row 3
...

I tried to create a multidimensional array, but all I get is undefined:

var triggers = [[cells[0], cells[1], cells[2]]];

is it possible to pass cells arrays' variables to triggers array? Can't figure it out?!

1 Answer 1

6

You can use slice to get part of an array, for example

var triggers = [cells.slice(0, 3)];

The call cells.slice(0, 3) returns an array with the elements of cells starting from index 0 up to and excluding 3, i.e. [cells[0], cells[1], cells[2]]. You can wrap another array over that "manually" to get the desired result.

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

2 Comments

Superb )) this is what i needed. Only one thing is left )) I also need to chekUp to diagonals )) cells[0] + cells[4] + cells[8] cells[2] + cells[4] + cells[6]
@A1exandr: That won't be possible with an one-liner unless you write it out manually.

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.