Skip to content

Instantly share code, notes, and snippets.

@leandroandrade
Created December 13, 2023 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandroandrade/5cd86764515129ab83e5da314ae70cad to your computer and use it in GitHub Desktop.
Save leandroandrade/5cd86764515129ab83e5da314ae70cad to your computer and use it in GitHub Desktop.
const dig = (n1, n2, n3, n4) => {
const nums = n1.split("").concat(n2.split(""), n3.split(""));
if (n4 !== undefined){
nums[9] = n4;
}
let x = 0;
for (let i = (n4 !== undefined ? 11:10), j = 0; i >= 2; i--, j++) {
x += parseInt(nums[j]) * i;
}
const y = x % 11;
return y < 2 ? 0 : 11 - y;
}
const aleatorio = () => {
const aleat = Math.floor(Math.random() * 999);
return ("" + aleat).padStart(3, '0');
}
const gerarCpf = () => {
const num1 = aleatorio();
const num2 = aleatorio();
const num3 = aleatorio();
const dig1 = dig(num1, num2, num3);
const dig2 = dig(num1, num2, num3, dig1);
// return `${num1}.${num2}.${num3}-${dig1}${dig2}`;
return `${num1}${num2}${num3}${dig1}${dig2}`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment