使用 Luhn 算法的信用卡检查器 javascript

Credit card checker with Luhn algorith javascript

我目前正在使用 Luhn 算法研究 codeacademy 的 'Credit Card Checker',但是我的代码在有效数组(应该 return)上是 returning false。你能帮我解决这个问题吗?

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];


// Add your functions below:

const validateCred = arr => {
  let totalSum = 0;
  let revList = arr.reverse();
  for (let i = 0; i < revList.length; i++) {
    let calcAmount = revList[i];
    if (i !== 0 && i%2 === 0) {
      calcAmount = revList[i] * 2;
      if (calcAmount > 9 ) {
        calcAmount -= 9;
        totalSum += calcAmount;
      } else {
        totalSum += calcAmount;
      }
    } else {
      totalSum += revList[i];
    }
  }
  return (totalSum%10 === 0 ? true : false);
};

console.log(validateCred(valid3))
console.log(validateCred(valid4))
console.log(validateCred(valid5))

重写并修复。

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];


function luhnCheck(num) {
  let digit, j, len, odd = true, sum = 0
  const digits = (num + '').split('').reverse()
  for (j = 0, len = digits.length; j < len; j++) {
    digit = parseInt(digits[j], 10)
    if ((odd = !odd)) digit *= 2
    if (digit > 9) digit -= 9
    sum += digit
  }
  return sum % 10 === 0
}

batch.forEach((item) => {
 let number = item.join('')
 console.log(number, luhnCheck(number))
})

理想情况下,您应该将其作为字符串传入,并让函数进行拆分(关注点分离),就像在真正的应用程序中一样,您希望在 运行ning luhn 之前对输入的数字执行各种操作像.. cardTypeFromNumber (visa, maestro, dankort, mastercard, amex, dinersclub, discover, chinaunionpay (doesn't use luhn), jcb 大多有所有不同的长度你不想 运行 luhn 直到你验证长度和格式)、formatCardNumber、validateCardExpiry、validateCardCVC 等等。

一个简单的错误,检查if (i%2 === 1) {,算法需要为每个第二个元素乘以2

arr.reverse() 也是个坏主意,原来的数组被改变了。

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];


// Add your functions below:

const validateCred = arr => {
  let totalSum = 0;
  let revList = arr.reverse();
  for (let i = 0; i < revList.length; i++) {
    let calcAmount = revList[i];
    if (i%2 === 1) {
      calcAmount = revList[i] * 2;
      if (calcAmount > 9 ) {
        calcAmount -= 9;
        totalSum += calcAmount;
      } else {
        totalSum += calcAmount;
      }
    } else {
      totalSum += revList[i];
    }
  }
  return(totalSum%10 === 0 ? true : false);
};

batch.forEach(c => console.log(validateCred(c)));

Luhn algorithm 在维基百科上有解释。其他人已经回答了这个问题,这只是一个不同的实现。

function luhnCheck(cardNumber) {
  let nums = cardNumber.split('').reverse();
  let checkValue = nums.shift();
  let luhnSum = nums.reduce((sum, n, i) => {
    let val = n*(i % 2? 1 : 2);
    sum += val > 9? val - 9 : val;
    return sum;
  }, 0);
  return checkValue == 10 - (luhnSum % 10);
}


['4539677908016808', // valid1
 '5535766768751439', // valid2
 '4532778771091795', // invalid1
 '5795593392134643', // invalid2
 '344801968305414',  // mystery1
 '5466100861620239', // mystery2
].forEach(cardNum =>
   console.log(luhnCheck(cardNum))
);