Array.pop is not a function

Track : Full Stack
error: array.pop is not a function

Please explain to me like a child, what have I done that I shouldn’t do??

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];

    const validateCred = array => {
        // variable declaration
        let poppedValue = [];
        // pop the last digit of the array
        poppedValue = array.pop();
        // reverse the array
        array.reverse();
        // Multiply the digits in oddPosition
        let multipliedArray = array.map((e, index) => index % 2 !==0 ? e * 2 : e);
        // subtruct 9 from values > 9
        multipliedArray = multipliedArray.map(e => e > 9 ? e - 9 : e);
        // add all the values in the array including the popped Array
        // first concat the array with the popped value
        multipliedArray = multipliedArray.concat(poppedValue);
        // Add all the values in the multiplied array
        multipliedArray = multipliedArray.reduce((accumulator, currentValue) => accumulator + currentValue);
        // check value % 10
        multipliedArray = multipliedArray % 10 === 0 ? 'Valid' : 'Invalid';
    }
    
    // innitialize an empty array to save all invalid cards
    let invalidCardArray = [];
    
    // create a function to loop through all cards
const findInvalidCards = invalidCards => {
    for(let i = 0; i < invalidCards.length; i++){
        if(validateCred(invalidCards[i] === 'Invalid')){
            invalidCardArray.unshift(invalidCards);
        }
    }
    return invalidCardArray;
}
findInvalidCards(batch);
    

ERROR!!

I’ll just outline a few things.

Consider thinking about your function composition.

This is the way you have it structured:

  • validateCred takes an array (does it return anything? If not what does it do?)

  • findInvalidCards take an array and returns an array.

So when you write a line like:
if(validateCred(invalidCards[i] === 'Invalid')), there are 2 issues:

  • one, you’re using invalidCards[i] === 'Invalid' as an entire argument
  • second, if you ran if(validateCred(invalidCards[i]) === 'Invalid')) (key distinction: the argument is now only invalidCards[i]) you wouldn’t get a return value (because that function doesn’t have a return value).
1 Like

Log out that parameter to confirm it is an array.

2 Likes

The initial problem is gone, now am only receiving an empty array ==>> Point me towards the right direction again

const validateCred = array => {
        // variable declaration
        let poppedValue = [];
        // pop the last digit of the array
        poppedValue = array.pop();
    
        // reverse the array
        array.reverse();
        // Multiply the digits in oddPosition
        let multipliedArray = array.map((e, index) => index % 2 !==0 ? e * 2 : e);
        
        // subtruct 9 from values > 9
        multipliedArray = multipliedArray.map(e => e > 9 ? e - 9 : e);
        // add all the values in the array including the popped Array
        // first concat the array with the popped value
        multipliedArray = multipliedArray.concat(poppedValue);
        // Add all the values in the multiplied array
        multipliedArray = multipliedArray.reduce((accumulator, currentValue) => accumulator + currentValue);
        // check value % 10
        multipliedArray = multipliedArray % 10 === 0 ? 'Valid' : 'Invalid';
        return multipliedArray;
    }
   
    
    // innitialize an empty array to save all invalid cards
    let invalidCardArray = [];
    // create a function to loop through all cards
const findInvalidCards = invalidCards => {
   
    for(let i = 0; i < invalidCards.length; i++){
       if(validateCred(invalidCards[i]) === 'Invalid'){
             invalidCardArray.push(invalidCards[i]);
         }
     }
    return invalidCardArray;
    
 }

 console.log(invalidCardArray);
 findInvalidCards(batch);