Pop error when using my function in another function

Hello so I am working on the credit card Checker project and have completed the validateCred function. Inside i use the pop function to grab the last number in the array before I start to work the problem. this fuction works great by its self. but when I call it findInvalidCards it throws the following error.
let checkDigit = cardNumber.pop();
^

TypeError: Cannot read property ‘pop’ of undefined
at validateCred (D:\credit-card-checker-starter\main.js:30:33)
dose any one have an idea on why this is happening.

here is my code

const validateCred = array => {
    let cardNumber = array;
    let checkDigit = cardNumber.pop(); 
    let isDouble = true;
    let cardReverse = cardNumber.reverse(); 
    let sum = 0;
    for(let i = 0; i <= cardReverse.length -1; i++){
    
        let num = cardReverse[i];
       
        if(isDouble === true){
           
            if (num * 2 > 9){
               
                sum += (num *2) -9;
            } else{
                sum += num * 2;
            }
        } else{
        sum += num;
                }      
        isDouble  = !isDouble;
    }
       sum += checkDigit;
        console.log(sum);
        return sum % 10 === 0
    
}
console.log(validateCred(invalid1))
 

const findInvalidCards = (nArray) => {

 let  invalidArray =[];

    for(let i = 0; i <= nArray.length; i++){
     let isValid = validateCred(nArray[i]);
     if (isValid === false){
         invalidArray.push(nArray[i]);
     }
    }
    return invalidArray;
}

console.log(findInvalidCards(batch))
1 Like

Your code should work perfectly and I think you did a great job overall.

There is just one of those tiny issues that have a large effect. Take a look inside the findInvalidCards function definition. When you create the loop there. There’s this line:

    for (let i = 0; i <= nArray.length; i++) {

Maybe by seeing this you already noticed it, but in case you didn’t, just think… what will be the last loop cycle there? When i equals the length of nArray. Now, what array of nArray occupies the position nArray[nArray.length] ?

I don’t want to take away from you the pleasure of solving it :slight_smile: so I won’t tell you anything else, but if you still have doubts about this or something related, do follow up.

1 Like