Commit 4f85c909 authored by Yannick Li's avatar Yannick Li
Browse files

Replace checkArray() with every()

parent 933c4b58
......@@ -234,16 +234,16 @@ class Grid extends React.Component<IGridProps, IGridState> {
*/
validateLine(line: number) {
assert.ok(line >= 0 && line < 9)
let check = Array(9).fill(0)
let cpt = Array(9).fill(0)
for (let column = 0; column < 9; column++) {
let index = line * 9 + column
let val = this.state.cells[index][0]
if (val.length === 0 || val.length > 1) {
continue
}
check[Number(val)-1]++
cpt[Number(val)-1]++
}
return checkArray(check)
return cpt.every((c) => c <= 1)
}
/**
......@@ -252,16 +252,16 @@ class Grid extends React.Component<IGridProps, IGridState> {
*/
validateColumn(column: number) {
assert.ok(column >= 0 && column < 9)
let check = Array(9).fill(0)
let cpt = Array(9).fill(0)
for (let line = 0; line < 9; line++) {
let index = line * 9 + column
let val = this.state.cells[index][0]
if (val.length === 0 || val.length > 1) {
continue
}
check[Number(val)-1]++
cpt[Number(val)-1]++
}
return checkArray(check)
return cpt.every((c) => c <= 1)
}
/**
......@@ -270,16 +270,16 @@ class Grid extends React.Component<IGridProps, IGridState> {
*/
validateBlock(block: number) {
assert.ok(block >= 0 && block < 9)
let check = Array(9).fill(0)
let cpt = Array(9).fill(0)
let indexList = blockIndex(block)
for (let index of indexList) {
let val = this.state.cells[index][0]
if (val.length === 0 || val.length > 1) {
continue
}
check[Number(val)-1]++
cpt[Number(val)-1]++
}
return checkArray(check)
return cpt.every((c) => c <= 1)
}
/**
......@@ -398,19 +398,6 @@ function generateStaticGrid() {
return values;
}
/**
* Check if a array only contains values 0 or 1.
* @param array The array to be checked.
*/
function checkArray(array:any) {
for (let i = 0; i < array.length; i++) {
if (array[i] !== 0 && array[i] !== 1) {
return false
}
}
return true
}
/**
* Return an array containing all cell index of a block.
* @param block The block number of which we want the cells index.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment