Luhn algorithm: Difference between revisions

Content deleted Content added
add typescript example
Line 160:
</syntaxhighlight>
 
=== [[TypeScript]] ===
<syntaxhighlight lang="typescript" line="1">
function luhnCheck(input: number): boolean {
const number = input.toString();
const digits = number.replace(/\D/g, '').split('').map(Number);
let sum = 0;
let isSecond = false;
for (let i = digits.length - 1; i >= 0; i--) {
let digit = digits[i];
if (isSecond) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
isSecond = !isSecond;
}
return sum % 10 === 0;
}
</syntaxhighlight>
== Uses ==
The Luhn algorithm is used in a variety of systems, including: