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().