Last active
June 12, 2017 14:53
-
-
Save fatso83/a849ca8a75eb4b5e35cd1af700435875 to your computer and use it in GitHub Desktop.
Generates Norwegian FH numbers (Felles Hjelpenummer) - 11 digit ids used at hospitals for tourists and people without ids
This file contains hidden or 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
// original: https://gist.github.com/a849ca8a75eb4b5e35cd1af700435875 | |
// The two checksum series. | |
// for digit 10, the first check digit | |
const checksumSeries1 = [3, 7, 6, 1, 8, 9, 4, 5, 2, 1]; // 10 digits | |
// for digit 11, the 2nd check digit | |
const checksumSeries2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1]; // 11 digits | |
function sumDigits(number, checksumSeries) { | |
// Split into array of digits | |
const digits = number.toString().split('').map(d => parseInt(d, 10)); | |
let sum = 0; | |
for (let i = 0; i < checksumSeries.length; ++i) { | |
sum += digits[i] * checksumSeries[i]; | |
} | |
return sum; | |
} | |
function generateValidNumbers(startingDigitString){ | |
const digitsToGenerate = 11 - startingDigitString.length; | |
const numberSeriesUpperBound = Math.pow(10, digitsToGenerate); | |
let id; | |
let i = Math.pow(10, digitsToGenerate - 1); | |
while(i++ < numberSeriesUpperBound) { | |
id = startingDigitString + i; | |
const rest1 = sumDigits(id, checksumSeries1) % 11 | |
if(rest1 === 0) { | |
const rest2 = sumDigits(id, checksumSeries2) % 11 | |
if(rest2 === 0) | |
console.log(id); | |
} | |
} | |
} | |
function generateFhNumbers(){ | |
generateValidNumbers('8'); | |
} | |
function generateNormalNationalIds() { | |
// just generate | |
} | |
generateFhNumbers(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment