JSFiddle - React, Tailwind, and code Playground

by alexb

JavaScript

function luhnCheckValid(accNo) {
  let total = 0;
  for (let i = 1; i <= accNo.length; i++) {
  	const digit = accNo[accNo.length - i];
    const n = parseInt(digit, 10);
    if (i % 2 > 0) {
    	total += n;
    }
    else if (n > 4) {
    	total += n*2 - 9;
    }
    else {
    	total += n*2;
    }
  }
  return total % 10 == 0;
}