2 Replies - 416 Views - Last Post: 21 April 2013 - 12:09 AM

#1 zhenurik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-March 13

Some array iteration problem.

Posted 20 April 2013 - 01:12 PM

Function getRowWin(arr) supposed to find '4 in a raw' - 4 the same elements together in one line in the 2 dimensional array. But for some reason it does not work.

matrix = [  [1,2,3,4,5,6,7],
            [2,3,1,7,1,1,1],
            [3,2,3,4,5,6,4],
            [4,2,4,4,4,4,5],
            [5,2,3,4,5,6,7],
            [6,2,2,2,2,6,8]
            ];
function getRowWin(arr){
    
    for (var i = 0; i < 6; i++){
        for (var y = 0; y < 4; y++){
            if( arr[i][y] ==  arr[i][y+1] ==  arr[i][y+2]  ==  arr[i][y+3]){
                 console.log(arr[i][y]+' '+arr[i][y+1]+' '+arr[i][y+2]+' '+arr[i][y+3]);
            }else
                console.log("Nothing");
        }
    }
}
getRowWin(matrix);


Is This A Good Question/Topic? 0
  • +

Replies To: Some array iteration problem.

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3355
  • View blog
  • Posts: 11,365
  • Joined: 12-December 12

Re: Some array iteration problem.

Posted 20 April 2013 - 01:40 PM

You need ands:

if ( arr[i][y] ==  arr[i][y+1] && arr[i][y+1] ==  arr[i][y+2]
    && arr[i][y+2]  ==  arr[i][y+3]) {

Was This Post Helpful? 1
  • +
  • -

#3 zhenurik  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-March 13

Re: Some array iteration problem.

Posted 21 April 2013 - 12:09 AM

andrewsw, THANK YOU!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1