luhn-alg

node v14.20.1
version: 0.1.0
endpointsharetweet
const LuhnAlgorithm = num=> { const inputNum = num.toString(); let sum = 0; let doubleUp = false; /* from the right to left, double every other digit starting with the second to last digit.*/ for (let i = inputNum.length - 1; i >= 0; i--) { let curDigit = parseInt(inputNum.charAt(i)); /* double every other digit starting with the second to last digit */ if (doubleUp) { /* doubled number is greater than 9 than subtracted 9 */ if (curDigit * 2 > 9) { sum += curDigit * 2 - 9; } else { sum += curDigit * 2; } } else { sum += curDigit; } doubleUp = !doubleUp; } return sum % 10 == 0 ? true : false; }
LuhnAlgorithm(4111111111111111);
LuhnAlgorithm(4111111111111110);
Loading…

no comments

    sign in to comment