Skip to content

Instantly share code, notes, and snippets.

@alexsad
Created June 27, 2024 13:03
Show Gist options
  • Save alexsad/f654f2c898989b0e52f606301e3128f2 to your computer and use it in GitHub Desktop.
Save alexsad/f654f2c898989b0e52f606301e3128f2 to your computer and use it in GitHub Desktop.
cpf validation
const sumCPF = (cpf, digitos) => {
const sum = cpf
.split('')
.filter((...[,index]) => index < digitos)
.reduce((sum, curr, index) => sum + (curr * (1+digitos - index)), 0);
const total = (sum * 10) % 11;
return [10,11].includes(total) ? 0 : total;
}
sumCPF('62193597049', 9); //first digit verification
sumCPF('62193597049', 10); //second digit verification
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment