Credit Card Checker

Hey guys, I’m trying to figure out step #5 of this project and I’m unsure of what I’m doing with my code. Will really appreciate the help! :pray:
Link to the project (https://www.codecademy.com/practice/projects/credit-card-checker)

This is what I currently have.

const idInvalidCardCompanies = invldCompanies => {
    let companies = [];
    for (k = 0; k < invldCompanies.length; k++) {
        if (invldCompanies[k].substring(0, 1) === '3') {
            companies.push('Amex (American Express)');
        } else if (invldCompanies[k].substring(0, 1) === '4') {
            companies.push('Visa');
        } else if (invldCompanies[k].substring(0, 1) === '5') {
            companies.push('Mastercard');
        } else if (invldCompanies[k].substring(0, 1) === '6') {
            companies.push('Discover');
        } else {
            return 'Company not found!';
        }
    }
}
idInvalidCardCompanies(invalidCred);
console.log(companies);

One hint: What does return do?

1 Like

Hey @janneslohmeijer, basically it’s to pass back information to the function?

1 Like

return breaks of the whole function, but do you want that when iterating through a whole array? In this case the first time it meets a card that is not of a company it will stop doing what it did.

Instead you would want it pushed to the same array where you store the company names.

Also, substring method is used on strings not on arrays. Isn’t batch made out of arrays?

Oh yeah. Thanks for that @janneslohmeijer. And yeah, batch is made out of arrays. The trouble I’m having right now is, comparing the first digit of the element to that of the credit card companies. I’m unsure of what built in objects I’m able to use. These are what I have so far.

const validateCred = card => {
    const arr = [];
    for (i = 0; i < card.length; i++) {
        if (i % 2 === 0) {
            if (card[i]*2 < 10) {
                arr.push(card[i]*2);
            } else {
                arr.push(card[i]*2-9);
            }
        } else {
            arr.push(parseInt(card[i], 10));
        }
    }
    return arr.reduce( (prv, cur) => prv + cur) % 10 === 0;
}
console.log(validateCred(valid3));


let validCred = [];
let invalidCred = [];
const findInvalidCards = invlCards => {
    for (j = 0; j < invlCards.length; j++) {
        if (validateCred(invlCards[j])) {
            validCred.push(invlCards[j]);
        } else {
            invalidCred.push(invlCards[j]);
        }
    }
}
findInvalidCards(batch);
console.log(invalidCred);


let companies = [];
const idInvalidCardCompanies = invldCompanies => {
    for (k = 0; k < invldCompanies.length; k++) {
        if (invldCompanies[k].substring(0, 1) === '3') {
            companies.push('Amex (American Express)');
        } else if (invldCompanies[k].substring(0, 1) === '4') {
            companies.push('Visa');
        } else if (invldCompanies[k].substring(0, 1) === '5') {
            companies.push('Mastercard');
        } else if (invldCompanies[k].substring(0, 1) === '6') {
            companies.push('Discover');
        } else {
            return 'Company not found!';
        }
    }
}
idInvalidCardCompanies(invalidCred);
console.log(companies);

You are on the right track :wink: . Using an if…else if…else statement is ok, subsequently you could use a switch but it doesn’t really matter.

What you need to do now is read the proper digit from the array.

Question you should ask yourself in aiding towards the solution is:

  • What type of variable am I dealing with, string, array, array of numbers, array of strings?
3 Likes

Hey @janneslohmeijer! I did it! You made me realize that I’m dealing with the array of numbers, so I quickly switched out the strings that I had in the if…else if statement. And as I read more on MDN regarding the “two-dimensional array”, found out that I can access the first digit of the array of numbers! Really appreciate your help a lot :pray::pray:

Now I’m just trying to figure out the last part of the exercise telling us that,
“This array should NOT contain duplicates, i.e. even if there are two invalid Visa cards, "Visa" should only appear once in the array.”
I suppose I have to use the .filter() method?

1 Like

Lol, I must have missed that part :stuck_out_tongue: . So I just did it, but not by using .filter().

nicely thought out function. can someone explain why taking the modulo 2 for each “i” will never reach the last number(unique digit) in the card.length? Is it because the operator is set to i<card.length ?

Is it correct to assume that no matter the length, the < operator will never reach the last “i” number? Thanks.

Switches have less repeated code in them, but still quite a lot, if one keeps stripping away repeated stuff then it’s looking a whole lot like an object right?

  {
    3: 'Amex (American Express)',
    4: 'Visa',
    5: 'Mastercard',
    6: 'Discover'
  }

… oh ancient thread.

Still, I will crusade against switches.

1 Like

Is that really true though?

Let’s say an array has a length of 11.
The last index of that is 10, which is 0 mod 2

Or, for an array of length 10, the last index is 1 mod 2

If you’re talking about loops in general (having nothing to do with modular arithmetic) then, what do the different parts in a for-statement do, and what will the final therefore be?
In that case, what do you mean by last number? last number of which numbers?
If you iterate through an array you would probably want to visit each element, including the last one, given that you’re probably counting up from 0, when would you need to stop?

1 Like