Credit Card Checker

Good morning everyone, I have been pretty stuck on the credit card checker project for some time and I would like to get some feedback on what is going wrong with the code. Here is the a link to the project: https://www.codecademy.com/practice/projects/credit-card-checker

I’m so lost and confused on the project I’m just at a loss.

Here is the 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:
const validateCred = card => {
  let newCreditCard = [...card.reverse()];
  for (let i = 0; i < newCreditCard.length; i++) {
    if (i % 2 != 0) {
      newCreditCard[i] = newCreditCard[i] * 2;
    }
    if (newCreditCard[i] > 9) {
      newCreditCard[i] -= 9;
    }
  }
  let sum = newCreditCard.reduce((a, b) => a + b, 0);
  if (sum % 10 === 0) {
    return true;
  } else {
    return false;
  }
};

const findInvalidCards = cards => {
  let invalidCards = [];
  for (let j = 0; j < cards.length; j++) {
    if (validateCred(cards) === false) {
      invalidCards.push(cards);
    }
  }
};

How does the code work differently from what you expect?

I would highly recommend to insert some function calls:

const validateCred = card => {
  let newCreditCard = [...card.reverse()];
  for (let i = 0; i < newCreditCard.length; i++) {
    if (i % 2 != 0) {
      newCreditCard[i] = newCreditCard[i] * 2;
    }
    if (newCreditCard[i] > 9) {
      newCreditCard[i] -= 9;
    }
  }
  let sum = newCreditCard.reduce((a, b) => a + b, 0);
  if (sum % 10 === 0) {
    return true;
  } else {
    return false;
  }
};

console.log(validateCred(valid1));

and test the cards. Or make a loop, makes a lot easier to test cards:

for (let i = 0; i < batch.length; i++){
    console.log(validateCred(batch[i]));
}

your validateCred seems good.

findInvalidCards also seems good, i don’t seen any obvious flaws. Again, you could add function calls and console.log() to inspect your code

What is this?

And what are you overall experiencing that is not going well?

@stetim94 and I have just determined that

should be


[...card].reverse();

To add to @janneslohmeijer, first you need to make a copy of the array, then you can reverse the copy. Otherwise, you reverse the original array first and then make a copy (which then of course is also reversed)

but we can’t reverse the original array

Thanks for all the replies. I fixed my typo within my spread to copy the arrays. I know validateCred() worked with console.log but when I try to say console.log(findInvalidCards(invalid1); it just loops invalid1 over and over again.

// 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 = card => {
  let newCreditCard = [...card].reverse();
  for (let i = 0; i < newCreditCard.length; i++) {
    if (i % 2 != 0) {
      newCreditCard[i] = newCreditCard[i] * 2;
    }
    if (newCreditCard[i] > 9) {
      newCreditCard[i] -= 9;
    }
  }
  let sum = newCreditCard.reduce((a, b) => a + b, 0);
  if (sum % 10 === 0) {
    return true;
  } else {
    return false;
  }
};

const findInvalidCards = cards => {
  let invalidCards = [];
  for (let j = 0; j < cards.length; j++) {
    if (validateCred(cards) === false) {
      invalidCards.push(cards);
    }
  }
  return invalidCards;
};

console.log(findInvalidCards(invalid1));

Davids-MacBook-Pro:credit-card-checker-starter techlab$ node main.js
[
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ],
  [
    4, 5, 3, 2, 7, 7,
    8, 7, 7, 1, 0, 9,
    1, 7, 9, 5
  ]
]
Davids-MacBook-Pro:credit-card-checker-starter techlab$ 

here:

const findInvalidCards = cards => {

you see cards (plural) as parameter, combined with the fact that you see a loop, you should pass an array as argument to this function:

console.log(findInvalidCards([invalid1]));

That is what i like about language like Java, you can specify parameter, so they something like:

array cards

this tell you, this function expects an array.

Take a close look at your for look in findInvalidCards :wink:

What are you checking? The whole array or a card of your array of cards?

i would think im checking each card in an array of cards correct?

I don’t know, I just feel lost. nothing is clicking and sticking.

True, you need to pass an array of cards, for example:

console.log(findInvalidCards(batch));

batch is an array of cards, you shouldn’t pass an individual card to this function

when i pass only batch I am receiving undefined in the console…

can you post your full code of attempting this?

i reformatted my code because I didn’t like the way i was writing variables…

// 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 = validCredArr => {
  let newValidCredArr = [...validCredArr].reverse();
  for (let i = 0; i < newValidCredArr.length; i++) {
    if (i % 2 != 0) {
      newValidCredArr[i] = newValidCredArr[i] * 2;
    }
    if (newValidCredArr[i] > 9) {
      newValidCredArr[i] -= 9;
    }
  }
  let sum = newValidCredArr.reduce((a, b) => a + b, 0);
  if (sum % 10 === 0) {
    return true;
  } else {
    return false;
  }
};

const findInvalidCards = badCard => {
  let invalidCards = [];
  for (let j = 0; j < badCard.length; j++) {
    if (validateCred(badCard) === false) {
      invalidCards.push(badCard);
    }
  }
};

console.log(findInvalidCards(batch));

and in this process you seemed to have removed the return statement:

return invalidCards;
1 Like

so this is where im really confused. its just looping through all of batch, its not selecting the incorrect values

why not? Here:

 if (validateCred(badCard) === false) {
      invalidCards.push(badCard);
    }

that is happening.

You are currently running the whole batch through validateCred which will cause it to fail. You validateCred expects a single card array of numbers not an array of cards.

So, how do you only check a single card from the batch?

Hint: the batch is an array, and how do you acces a single item in an array?

I hope you find out yourself. If you really can’t get to it reveal thespoiler down below.

Summary

You still need to select the proper card of your batch using iteration counter j. Run it through validateCard and push only that card.

 if (validateCred(badCard[j]) === false) {
      invalidCards.push(badCard[j]);
    }

Thank you all for your replies. You have all been very helpful. Would anyone like to be my mentor? lol

1 Like

what is ...card

What do the dots mean?

destructing assignment:

1 Like