Skip to content

Instantly share code, notes, and snippets.

@karl-gustav
Last active March 9, 2018 11:03
Show Gist options
  • Save karl-gustav/9f983efb82de6adfe4e49923308526ff to your computer and use it in GitHub Desktop.
Save karl-gustav/9f983efb82de6adfe4e49923308526ff to your computer and use it in GitHub Desktop.
validate if number is valid Norwegian organisation number
function isOrgNumberValid(orgNumber) {
if (orgNumber.toString().length != 9) {
return false;
}
const wheightNumbers = [3, 2, 7, 6, 5, 4, 3, 2];
const orgNumbers = orgNumber.toString().split('');
const controlDigit = orgNumbers[orgNumbers.length - 1]
const result = wheightNumbers.map((n, i) => n * orgNumbers[i]).reduce((a, b) => a+b, 0)
const rest = result % 11;
return +controlDigit === rest;
}
@karl-gustav
Copy link
Author

Debug version:

function isOrgNumberValid(orgNumber) {
	if (orgNumber.toString().length != 9) {
		return "wrong length";
	}
	const wheightNumbers = [3, 2, 7, 6, 5, 4, 3, 2];
	const orgNumbers = orgNumber.toString().split('');
	const controlDigit = orgNumbers[orgNumbers.length - 1]
	const result = wheightNumbers.map((n, i) => n * orgNumbers[i]).reduce((a, b) => a+b, 0)
	const rest = result % 11;
	return +controlDigit + " === " + rest;
}

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