Skip to content

Instantly share code, notes, and snippets.

@aichholzer
Last active July 14, 2023 21:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aichholzer/de4c40bac4879210eff42756ad113221 to your computer and use it in GitHub Desktop.
Save aichholzer/de4c40bac4879210eff42756ad113221 to your computer and use it in GitHub Desktop.
Validación de cédulas y similares (Algoritmo de Luhn)
const ced = '0931811087';
let [suma, mul, chars] = [0, 1, ced.length];
for (let index = 0; index < chars; index += 1) {
let num = ced[index] * mul;
suma += num - (num > 9) * 9;
mul = 1 << index % 2;
}
if ((suma % 10 === 0) && (suma > 0)) {
console.log('Pasa.');
} else {
console.error('No pasa.');
}
const ced = '0931811087'.split('');
let mul = 1;
const suma = ced.reduce((acc, num, index) => {
num *= mul;
mul = 1 << index % 2;
return acc * 1 + (num - (num > 9) * 9);
});
if ((suma % 10 === 0) && (suma > 0)) {
console.log('Pasa.');
} else {
console.error('No pasa.');
}
const ced = '0931811087';
let [suma, mul, index] = [0, 1, ced.length];
while (index--) {
let num = ced[index] * mul;
suma += num - (num > 9) * 9;
mul = 1 << index % 2;
}
if ((suma % 10 === 0) && (suma > 0)) {
console.log('Pasa.');
} else {
console.error('No pasa.');
}
@aichholzer
Copy link
Author

@Bernix01
Copy link

https://jsperf.com/cedula-while-for-reduce

not valid, can you update it pls?

@thianlopezz
Copy link

Hola intente usar for para la validacion pero no funciona con algunos numeros de cedula Sobretodo de Galapagos

@sally342
Copy link

help me with
def

@lgamerobles
Copy link

does anyone have an example with ruc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment