Created
July 2, 2022 22:58
-
-
Save Nick-Gabe/3ca33df5c431373695099f5b70597f59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// quando o usuário inserir um número, executa: | |
// pega a linha e coluna com base no id | |
const line = Math.floor(this.id / 9) | |
const row = this.id % 9 | |
// verifica se existe algum número igual a ele na vertical | |
const verifyVertical = board[line].includes(this.value) | |
// verifica se existe algum número igual a ele na horizontal | |
const verifyHorizontal = board.some((line) => line[row] === this.value) | |
// verifica se existe algum número igual a ele no grupo (quadrado) | |
const verifyBlock = board | |
.slice(Math.floor(line / 3) * 3, Math.ceil(line / 3) * 3 + 3) | |
.some((line) => { | |
return [line[row - 1], line[row], line[row + 1]].includes(this.value) | |
}) | |
// caso exista algum número igual, então este está errado | |
if (verifyVertical || verifyHorizontal || verifyBlock) { | |
return 'Número inválido' | |
} | |
// caso não exista, ele é válido | |
return 'Número válido' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment