Skip to content

Instantly share code, notes, and snippets.

@paulocanedo
Last active March 22, 2019 12:15
Show Gist options
  • Save paulocanedo/91c0ce01e770d878a22f70f1f8156e6a to your computer and use it in GitHub Desktop.
Save paulocanedo/91c0ce01e770d878a22f70f1f8156e6a to your computer and use it in GitHub Desktop.
função em JS para validar CPF
function calcularDigitoVerificador(numero) {
const mod11 = (value) => `${value}`
.split('')
.map(elem => Number.parseInt(elem))
.reduce((sum, elem, index) => sum + elem * (index + 1)) % 11;
let dv1 = mod11(numero.substring(0, 9));
dv1 = dv1 >= 10 ? 0 : dv1;
let dv2 = mod11(`${numero.substring(1, 9)}${dv1}`)
dv2 = dv2 >= 10 ? 0 : dv2;
return Number.parseInt(`${dv1}${dv2}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment