forked from flash6083/HacktoberFest-GUI-Projects-and-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.js
70 lines (57 loc) · 1.62 KB
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class Validate {
board;
boardSize;
sumTarget;
boxArray_fromBoard;
constructor(_board, _boardSize) {
this.board = _board;
this.boardSize = _boardSize;
this.sumTarget = parseInt(this.boardSize * (this.boardSize + 1) / 2)
}
runTests() {
return this.columnSums__validation() && this.rowSums_validation() && this.box_validation()
}
columnSums__validation() {
let isValid = true;
for (let i = 0; i < this.board.length; i++) {
let sum = 0;
for (let j = 0; j < this.board.length; j++) {
sum += this.board[j][i]
}
if (sum != this.sumTarget) {
isValid = false;
return isValid;
}
}
return isValid;
}
rowSums_validation() {
let isValid = true
this.board.forEach(x => {
if (x.reduce((a, b) => a + b, 0) != this.sumTarget) {
isValid = false
}
});
return isValid
}
box_validation() {
this.boxArray_fromBoard = generateBoxArray(this.board, this.boardSize)
let isValid = true
this.boxArray_fromBoard.forEach(x => {
isValid = this.unique(x)
});
return isValid;
}
unique(array) {
let x = array;
x.sort()
x = [...new Set(x)]
let sum = x.reduce((a, b) => a + b, 0);
if (sum != this.sumTarget || x.length != this.boardSize) {
return false
}
return true;
}
//TODO rowUnique_validation
//TODO columnUnique_validation
}