-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
50 lines (45 loc) · 1.28 KB
/
script.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
console.log('Hello World');
//452.458.458-45
function generateCPF() {
const number1 = generateRandomNumber();
const number2 = generateRandomNumber();
const number3 = generateRandomNumber();
const checkDigit1 = calculateCheckDigit(number1, number2, number3);
const checkDigit2 = calculateCheckDigit(
number1,
number2,
number3,
checkDigit1,
);
let cpf;
if (document.getElementById('dotsAndDashesTrue').checked) {
cpf = `${number1}.${number2}.${number3}-${checkDigit1}${checkDigit2}`;
} else {
cpf = `${number1}${number2}${number3}${checkDigit1}${checkDigit2}`;
}
document.getElementById('inputResult').value = cpf;
}
function generateRandomNumber() {
const random = Math.floor(Math.random() * 999);
const formatted = random.toString().padStart(3, '0');
return formatted;
}
function calculateCheckDigit(number1, number2, number3, number4) {
const numbers = `${number1}${number2}${number3}`.split('');
if (number4 !== undefined) {
numbers.push(number4);
}
let sum = 0;
let index = 0;
const initialNumber = number4 !== undefined ? 11 : 10;
for (let i = initialNumber; i >= 2; i--) {
sum += parseInt(numbers[index]) * i;
index++;
}
const rest = sum % 11;
if (rest < 2) {
return 0;
} else {
return 11 - rest;
}
}