Cannot read property of '0' of undefined

Hi there… I’ve been going roung in circles, the code works but i keep getting the following error and I just can’t figure out whats going wrong…

TypeError: Cannot read property ‘0’ of undefined
It is referring to the (IF statement arr[i][0])

Any ideas on how to solve this would be really appreciated :slight_smile:

const idInvalidCardCompanies = arr => {

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

  if (arr[i][0] === 3) {
   console.log('Amex');
   } 
  else if (arr[i][0] === 4){
    console.log('Visa');
  } else if (arr[i][0] === 5){
  console.log('Mastercard');
  } else if (arr[i][0] === 6){
  console.log('Discover'); 
  } else { console.log('Company not found');
  }
}

}

We cannot tell what the issue is just from the code. Can we see what value you are passing in as, arr?

Also, you can simplify the code since you are only reading the data, not mutating.

for (let x of arr) {
    n = x[0]
    if (n === 3) {

    } else if ...
}

Hi there… thank you… here is my code below:

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

// Add your functions below:

const validCred = (array32) => {
let sum1 = ;
let sum2 = ;

for (let i = array32.length-2; i>=0; i-=2) {
sum1.push(array32[i]);
} // array list of every other number starting from the end not including the first check digit

for (let i = array32.length-1; i>=0; i-=2) {
sum2.push(array32[i]);
} //array list of every odd number starting from the end

const double = sum1.map(x =>
x*2); // multiplying the even number array by two

const double2 = double // chaining iterator methods
.filter(n => n > 9) //filtering numbers above 9
.map(n => n - 9); // subtracting 9 from those numbers

// console.log(double2);

const double3 = double // filtering all the numbers less than 9
.filter(n=> n <=9);
// console.log(double3);

const finalSum1 = double2.concat(double3); // joining the two arrays togethor of the even numbers togethor

// console.log(finalSum1);

const finalSumAll = finalSum1.concat(sum2);

// console.log(finalSumAll);

const sumTotal = finalSumAll.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0);

// console.log(sumTotal);

finalAns = sumTotal % 10;

return finalAns;

}

const findInvalidCards = (array21) => {
let invalidCards = ;
let validCards = ;
for (let i=0; i<=array21.length-1; i++){
let check = validCred(array21[i]);

  if (check === 0) {
    validCards.push(array21[i]);
     } else { 
     invalidCards.push(array21[i]);
}

}
// return validCards;
return invalidCards;
}

function idInvalidCardCompanies(invalidBatch) {

for (let i = 0; i <= invalidBatch.length; i++) {
console.log(invalidBatch[i][0]);
if (invalidBatch[i][0] === 3) {
console.log(‘Amex’);
} else if (invalidBatch[i][0] === 4){
console.log(‘Visa’);
} else if (invalidBatch[i][0] === 5){
console.log(‘Mastercard’);
} else if (invalidBatch[i][0] === 6){
console.log(‘Discover’);
} else { console.log(‘Company not found’);
}
}
}

console.log(findInvalidCards(batch));

idInvalidCardCompanies(findInvalidCards(batch));

Might I recommend you redo all the loops that are read-only (as shown above). I also recommend using simple variable names inside the functions and loops. Once that is done, test your code and see what errors crop up. It is so much easier finding bugs in easy to read, simplified code.

Also, there is a way to post formatted code samples: At the top and bottom of your code block, write three back ticks on their own line. That will tell Discourse it is a code sample.

1 Like

Hi there… sorry about that… i will do that and get back to you

1 Like

Hi there, in fact by simplifying the code I fixed the problem.

Thank you for your help.

2 Likes