Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Why is this implementation of Luhn's algorithm not working?

I was creating a function for Luhn's algorithm in Javascript. Here is my code:

// 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:
function validateCred(arr){
  let newArr = [];
  newArr.push(arr[arr.length - 1]);
  for(let i = arr.length - 2; i >= 0; i--){
    let x = arr[i];
    // console.log(x);
    x *= 2;
    if(x > 9){
      x -= 9;
    }
    // console.log(x);
    newArr.push(x);
    // console.log(newArr);
  } 
  // console.log(newArr);
   let sum = 0;
   newArr.forEach(num => sum += num);
   console.log(sum);
//    if(sum % 10 === 0){
//     return true;
//   } else {
//      return false;
//  }
  return newArr;
}
console.log(validateCred(valid1));

This is not my original code, but as you can see validateCred is my function. The if statement is actually supposed to give a result of true, but it is giving false, so I was wondering where I went wrong. The console.log's in here were helping me figure out what went wrong, so here is the output of them. I cut the code to see what the function was doing with the array, I checked and everything was good. Even the sum was working. So I was left wondering whether the array is wrong, but when I inputed the same array in GeeksForGeeks's implementation, It gave true! So what went wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't want to take Niet's credits, so if he posts his comments as an anwser, you should accept his. I'm posting this only two show two shorter alternatives using Array.prototype.reduce

The main problem with your algorithm is (as Niet already said) you are doubling each digit, instead of only every other digit.

Variant 1 with reversing the card number

function checkCard(arr) {
  return arr.slice().reverse() 
    .reduce((a, c, i) => a + (i % 2 == 0
      ? c
      : (c < 5 ? 2 * c : 2 * c - 9)), 
      0) % 10 == 0;
}

I used arr.slice().reverse() because reverse works in place. So to keep arr in the original order, copy it first. reduce will then sum up the digits. For every even index (0,2,4,...) in the reversed array it will just add the current digit c. For every odd (1,3,5,...) index, it will add either 2*c or 2*c-9, depending on whether 2*c > 9.

Variant 2 without reversing the card number

function checkCard2(arr) {
  const p = arr.length % 2
  return arr.reduce((a,c,i) => a + (i % 2 != p 
    ? c
    :  (c < 5 ? 2 * c : 2 * c - 9)),
    0) % 10 == 0;
}

Because Luhn's original algorithm starts couting from the rightmost digit, if we start from the leftmost digit (ie without reversing the number), we have to act differently on numbers of even or odd length. For numbers of odd length, we must double the digits at odd indexes (1,3,5,...), for even length we must double the digits at even indexes (0,2,4,...). That's what the p is for. It will be 0 for even length, and 1 for odd length. So if i % 2 == p we must double the current digit, otherwise not.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!