luhnCheck implementation | JavaScript

December 24th, 2021 07:50:38 PM

					
const luhnCheck = num => { let arr = (num + '') .split('') .reverse() .map(x => parseInt(x)); let lastDigit = arr.splice(0, 1)[0]; let sum = arr.reduce( (acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val)), 0 ); sum += lastDigit; return sum % 10 === 0; }; luhnCheck('4485275742308327'); // true luhnCheck(6011329933655299); // true luhnCheck(123456789); // false

Featured Posts