Last active
July 14, 2023 21:37
-
-
Save aichholzer/de4c40bac4879210eff42756ad113221 to your computer and use it in GitHub Desktop.
Validación de cédulas y similares (Algoritmo de Luhn)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | |
} |
not valid, can you update it pls?
Hola intente usar for para la validacion pero no funciona con algunos numeros de cedula Sobretodo de Galapagos
help me with
def
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
https://jsperf.com/cedula-while-for-reduce