Skip to content

Instantly share code, notes, and snippets.

@petersg83
Created May 15, 2020 18:07
Show Gist options
  • Save petersg83/57f6a909ad8bd04b860335f60c2da895 to your computer and use it in GitHub Desktop.
Save petersg83/57f6a909ad8bd04b860335f60c2da895 to your computer and use it in GitHub Desktop.
const MULTIPLIERS = [1, 2, 1, 2, 1, 2, 1, 2, 1];
const isValid = (sin) => {
if (typeof sin !== 'string' || sin.length !== 9) {
return false;
}
splitedSin = sin.split('').map(Number);
const digits = splitedSin.reduce((allDigits, val, index) => {
const result = String(val * MULTIPLIERS[index]).split('');
return [...allDigits, ...result];
}, []);
const key = digits.reduce((sum, digit) => sum + Number(digit), 0);
return key % 10 === 0;
}
console.log(isValid('046454286'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment