Strange error: Credit card project

Hi, I am trying the credit card challenge project and I’m getting a weird error. My code is probably wrong but this error is showing after the last line within main.js. Any help? Also if someone could look through my code that would be great :slight_smile:. One thing I also wanted to know was if there is an inbuilt method for the Luhn algorithm. Thanks.

The code is:

// 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(array){
  for ( batchIndex = 0; batchIndex < batch.length; batchIndex ++){
    for (batch[batchIndex] = batch[batchIndex].length - 1; batch[batchIndex] >= 0; batch[batchIndex] --){
      if (batch[batchIndex] % 2 === 0 ){
        if (batch[batchIndex] * 2 > 9){
          return batch[batchIndex] - 9;
        }
        else if (batch[batchIndex] * 2 <= 9){
          return batch[batchIndex];
        }
              else { 
       return batch[batchIndex] = batch[batchIndex];
      }
       } 
    }
};

My error is:

this very likely means you forgot to close an if/for or function.

Always indent the code properly, makes it a lot easier to catch this kind of mistakes

1 Like

Hi, I’ve done a lot of work to that initial attempt but can’t seem to quite get it right. For the following code I should be getting a true, butI’m in fact getting a false. I console logged the sum and it appears that the sum is appearing as 16 for some reason. Code below:

// Add your functions below:
const validateCred = (card) => {
  let reverseCard = card.slice().reverse()
  for ( i = 0; i < reverseCard.length; i ++){
    if ( reverseCard[i] % 2 === 0){
      if ( reverseCard[i] > 9){
        reverseCard[i] = reverseCard[i] - 9;
      }
      else {
        reverseCard[i] = reverseCard[i] * 2;
      }
    }
    else {
      reverseCard[i] = reverseCard[i];
    }
    let sum = 0;
    sum += reverseCard[i];
    console.log(sum)
    if ( sum % 10 === 0){
      return true;
    }
    else {
      return false;
    }
  }
}
console.log(validateCred(valid1));

Have you checked this visual outline:

https://codecademy-content.s3.amazonaws.com/PRO/independent-practice-projects/credit-card-checker/diagrams/cc+validator+diagram+1.svg

your logic seems off.

What else do we now? How do we get to 16? Have you figured it out?

Looking at the valid1:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

we can see the last value is 8. What does this tell us?

1 Like

Thanks a lot, solved it with fresh eyes! :eyes:

1 Like