Skip to content

Instantly share code, notes, and snippets.

@lxndr-rl
Last active July 12, 2023 04:43
Show Gist options
  • Save lxndr-rl/64d3a1cc7825a0a3d8da0d7cab1b3c57 to your computer and use it in GitHub Desktop.
Save lxndr-rl/64d3a1cc7825a0a3d8da0d7cab1b3c57 to your computer and use it in GitHub Desktop.
[EC] Genera números de cédulas válidos
const generarCedula = () => {
const provincias = [
"01", "02", "03", "04", "05", "06", "07", "08", "09", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "30"
];
const tercerDigito = ["0", "1", "2", "3", "4", "5"];
const cuarto_noveno = Array.from({ length: 7 }, () => Math.floor(Math.random() * 10));
let cedula = provincias[Math.floor(Math.random() * provincias.length)] +
tercerDigito[Math.floor(Math.random() * tercerDigito.length)] +
cuarto_noveno.join('');
const digitos = Array.from(cedula, Number);
const suma = digitos.reduce((valorPrevio, valorActual, indice) => {
const multiplicador = (indice % 2 === 0) ? 1 : 2;
let resultado = valorActual * multiplicador;
if (resultado > 9) {
resultado -= 9;
}
return valorPrevio + resultado;
}, 0);
const verificador = (10 - (suma % 10)) % 10;
cedula += verificador;
return cedula;
};
console.log(generarCedula());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment