-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
42 lines (34 loc) · 934 Bytes
/
code.js
File metadata and controls
42 lines (34 loc) · 934 Bytes
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
function CPF(numero) {
let cpf = numero.replace(/[^\d]+/g, '');
let digitos = [];
let valorTotal = 0;
for (let i = 10, p = 0; i > 1 && p < 9; i--, p++) {
valorTotal += cpf[p] * i;
}
let digito = parseInt((valorTotal / 11) % 11);
if (digito < 2) {
digito = 0;
} else if (digito >= 2) {
digito = 11 - (valorTotal % 11);
}
digitos.push(digito);
valorTotal = 0;
for (let i = 11, p = 0; i > 1 && p < 10; i--, p++) {
valorTotal += cpf[p] * i;
}
digito = parseInt((valorTotal / 11) % 11);
if (digito < 2) {
digito = 0;
} else if (digito >= 2) {
digito = 11 - (valorTotal % 11);
}
digitos.push(digito);
if (digitos[1] == cpf[cpf.length - 1] && digitos[0] == cpf[cpf.length - 2]) {
console.log(`VALIDADOR DE CPF: ${numero} | Válido.`);
return true;
} else {
console.log(`VALIDADOR DE CPF: ${numero} | Inválido.`);
return false;
}
}
export default CPF;