File tree 1 file changed +8
-2
lines changed
1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change 1
1
var isValidSudoku = function ( board ) {
2
+ // create variable to store row , col and box values
2
3
let row = [ ]
3
4
let col = [ ]
4
5
let box = [ ]
6
+ // loop i over 0-8
5
7
for ( let i = 0 ; i <= 8 ; i ++ ) {
8
+ // loop j over 0-8
6
9
for ( let j = 0 ; j <= 8 ; j ++ ) {
10
+ // this will help us get position of each cell from board
7
11
let IndI = 3 * Math . floor ( i / 3 ) + Math . floor ( j / 3 )
8
12
let IndJ = 3 * ( i % 3 ) + ( j % 3 )
13
+ // check if value is already in row , col or box and return false
9
14
if ( row . indexOf ( board [ i ] [ j ] ) != - 1 ) { return false }
10
15
if ( col . indexOf ( board [ j ] [ i ] ) != - 1 ) { return false }
11
16
if ( box . indexOf ( board [ IndI ] [ IndJ ] ) != - 1 ) { return false }
12
-
17
+ // if not then add value to row , col and box
13
18
if ( board [ i ] [ j ] != '.' ) { row . push ( board [ i ] [ j ] ) }
14
19
if ( board [ j ] [ i ] != '.' ) { col . push ( board [ j ] [ i ] ) }
15
20
if ( board [ IndI ] [ IndJ ] != '.' ) { box . push ( board [ IndI ] [ IndJ ] ) }
16
21
}
22
+ // reset row , col and box array
17
23
row = [ ]
18
24
col = [ ]
19
25
box = [ ]
20
26
}
21
27
22
28
return true
23
- }
29
+ }
You can’t perform that action at this time.
0 commit comments