Credit Card Checker Challenge Project (JavaScript)

Here’s my solution:

Solved The Challenge ,

To access the Project Walkthrough video, click “Get Help” in the bottom-right corner.

Hello Everyone,

Here is my solution. Finally completed the project. I am just a beginner and believe that my code may not be that optimized. would really appreciate your inputs and feel free to let me know if there is any mistake in my code.

thank you

Hey Guys,

Here is my solution: https://github.com/achawn/credit_card_checker

My goodness. It took me many hours to complete this project, and definite moments of frustrations. It may not be the cleanest but it sure does work :slight_smile:

Hello everyone! I hope all of you are having fun coding.
This is my code from the Credit Card Project.

May the force be with all of you! :grinning:

Hi i’m struggling a lot to this exercise and any help would be really helpful !

I’m on step 4, and when I want to call the function findInvalidCars(); the console is logging the message : let lastE = array.pop();
^
TypeError: array.pop is not a function

here is my first step of the code

let validateCred = (array) => {
    let sum = 0;
    let sumTotal = 0;
    let lastE = array.pop();
    let arrayReverse = array.reverse();

    for(let i=0; i < arrayReverse.length; i+=2){
        arrayReverse[i] = arrayReverse[i]*2;
        if(arrayReverse[i] > 9) {
            arrayReverse[i] -= 9;
        }
    }
    console.log(arrayReverse);

    for (let j = 0; j < arrayReverse.length; j++) {
        sum += arrayReverse[j];
        } 
        console.log(sum);

    sumTotal = sum + lastE;
        console.log(sumTotal);
 
    if (sumTotal % 10 === 0) {
        return true;
    } else {
        return false;
    } 
};

    

const findInvalidCards = (nestArr) => {
    let invalidNum = [];
    for(let i = 0; nestArr.length > i; i++) {
        validateCred(nestArr[i]);
        if (validateCred(nestArr[i] === false)) {
            invalidNum.push(nestArr[i]);
        } else {
            validNum.push(nestArr[i]);
        }
    } 
    return invalidNum;
}
console.log(findInvalidCards(batch));

Do you see any error? When i call the first function , it’s working well and array.pop() is well taking out the last element without having this odd message.
thanks

nestArr[i] is not an array. Your function takes an array.

hi mtf,

I think i lost it but i don’t understand. Which argument should i add in this function ?

The terminal keep saying the array.pop() is not a function once i call the second function while it’s working well when i call the first one…

Is that an array of arrays? Might be I spoke to quickly, earlier.

Well when i read the step 4 :

Create another function, findInvalidCards() that has one parameter for a nested array of credit card numbers. The role of findInvalidCards() is to check through the nested array for which numbers are invalid, and return another nested array of invalid cards.

So i created that function with the parameter nested array but I actually have no idea of which nested array we are talking about ? Is it related to the variable “batch” ?

You are calling the invalidcards function with batch so one assumes that is an array of arrays, or array references. Is that correct?

Yes indeed by calling this function with batch as a reference I call an array which have some array nested inside. Is not correct the way I code it?
But i don’t understand why the terminal mentionned the only error is array.pop() which is working fine when I call the first function.

What you may need to do is comment out the lines in the loop and log out each array,

console.log(nestArr[i])

to confirm its data type.

Hi mtf, so this is my new code and I log out each array in the loop

const findInvalidCards = (nestArr) => {
    let invalidNum = [];
    for(let i = 0; nestArr.length > i; i++) {
        if(validateCred(nestArr[i]) === false) {
            invalidNum.push(nestArr[i]);
        }
        console.log(nestArr[i]);
    } 
    return invalidNum;
}
findInvalidCards(batch);

The output is each card number (14) “reworked” (reverse with the luhn algorithm to check wether is valid or not .

I think the code is correct because when i call the function and log it i have a nested array with only the invalid number !

Can you run the above and show us the output?

Here is my finally the final project that took way more than 40 minutes :smiley:

Please let me know if you see some inconsistency but all seems to work including the challenging last step!

Hello, @raphael_g. Nice job. There are a few things you might consider. Your code works as is, so feel free to disregard these suggestions.

  • Your stringToInt method is somewhat redundant in converting back and forth. Consider how you might chain methods together (split() & map()) to turn the string into an array of numbers in a single line of code.
  • In your validateCred method, is there really a need to pop() the first element of the array? Could you simply skip it? pop() forces the computer to do a lot of work under the hood. You are removing the value from the array, using reduce() to sum the array and then adding (summing) in the removed value. Why not leave the value in the array, and let reduce() sum it along with the other values?
  • Seems like the argument here: idInvalidCardCompanies(batch); should only be the invalid card numbers rather than all of the card numbers.

took some time to get to this point but I believe I did good