Some array iteration problem.

Page 1 of 1

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

#1 zhenurik

• New D.I.C Head

Reputation: 0
• 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

• But the opposite, you said.

Reputation: 5531
• Posts: 21,813
• 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

• New D.I.C Head

Reputation: 0
• 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

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}