Credit Card checker Exercise

Hello Everybody, i’m doing today the credit card checker exercise and i’m get stuck with the point 4.

To me, the findInvalidCards function is correctly working but when i run node, this does not produce any output, can anybody help me?

Thank you? I’m feeling so dumb as i know i missed something.

Here’s my code

const validateCred = cCNumber =>{
let cloneCcNumber = cCNumber.slice();
let checkNumber = cloneCcNumber.pop();
let reverseCcNumber = cloneCcNumber.reverse();
let validationArray = [];

const luhnAlgo = reverseCcNumber.map((x,i) =>{
    if(i% 2 === 0){
        let calc = x*2
        if (calc > 9){
              calc -= 9;
            return validationArray.push(calc)
        }else{
           return validationArray.push(calc)
        }
    }else{
        return validationArray.push(x);
    };
});
validationArray.push(checkNumber);

let total = validationArray.reduce((a,b)=>{
    return a+b;
})

if(total % 10 === 0){
    return true
}else{
   return false;
}
};


const findInvalidCards = ccArray =>{
    const invalidCards = [];
    for(let i = 0; i <= ccArray.length-1; i++){
        const card = ccArray[i];
        if (validateCred(card)=== false){
            return invalidCards.push(card)
            
        }
    }
    return InvalidCards;
};

findInvalidCards(batch);

Looking forward to your reply.
Kind regards

There is nothing in your code which will produce output to the console. return hands data back to the caller (the part of the program which calls the function), but it doesn’t print/log anything to the console.

Thank you for your reply, i know that at the moment there’s nothing that produce output to the console but even if i try to put instead of return invalidCards , console.log (invalidCards) it still print just the invalidCards array empty.

Why it does not push in the invalidcards?

Hi @gianmarcovarrone9307
First issue: You’re leaving the loop after the first push:

You don’t need the return keyword here.

Second issue: You’re returning an undefined variable.

1 Like

Hello @mirja_t and thank you ad everytime for your kind help.

  1. Question
    Why am i leaving the loop? Does return exit the loop?

  2. Question
    Why am i returning an undefined variable? What do you mean for undefined variable? Wasn’t defined when opening the function with const invalidCards = ; ?

Thank you so much.

Yes. From the MDN docs:

The return statement ends function execution and specifies a value to be returned to the function caller.

Take a close look at the spelling…

2 Likes

Oooh I’m feeling so dumb, this is a type error :man_facepalming:

Thank you so much for your help @mirja_t :raised_hands::raised_hands:

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.