Simple error I can't seem to figure out

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-challenge-project-credit-card-checker/projects/credit-card-checker

Displays ‘undefined’ instead of ‘mastercard’ or a list, everything runs and looks good.

Can anyone take a quick look?

Thank you, James

Hi there :slight_smile:

You need to copy and paste your code for us to know what’s wrong (see here for how to format your code: [How to] Format code in posts - #7)

Usually undefined in this context happens when some function isn’t called or used correctly. console.log statements are your friends.

2 Likes

Here’s the code below!

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// 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:

function validateCred (array){
let z = 0

do {
array = array[z] *2;
if (array[z] > 9){
array[z] = array[z]-9;
}
z = z + 2
} while (z < array.length)

let sum = 0;
for (let i = 0; i <= array.length; i++){
sum = sum + array[i];
}
if (sum % 10 === 0){
return true;
} else { return false; }

}

//loop for all arrays, then with calling, if false, add to another array and return it

function findInvalidCards(arrays) {
let invalid =
for (let t = 0; t <= arrays.length; t++){
if (validateCred.arrays[t] === false) {
invalid.push(arrays[t]);
}
}
return invalid;
}

//1 loop through invalid card numbers, 2 for each use if statment to check first number and add to array what it is

function idInvalidCardCompanies (invalidCred) {
let companiesArray =
for (let y = 0; y >= invalidCred.length; y++) {
if (invalidCred[y[0]] === 3) {
return companiesArray.push(‘Amex’)
} else if (invalidCred[y[0]] === 4) {
return companiesArray.push(‘Visa’)
} else if (invalidCred[y[0]] === 5) {
return companiesArray.push(‘Mastercard’)
} else if (invalidCred[y[0]] === 6) {
return companiesArray.push(‘Discover’)
} else {
return companiesArray.push(‘Company not found’)
}

return companiesArray.filter((value, index) => companiesArray.indexOf(value) === index);

}
}

console.log(idInvalidCardCompanies([invalid2]))

console.log(idInvalidCardCompanies(batch))
//BOTH RETURN UNDEFINED INSTEAD OF MASTERCARD AND A LIST

in the function idInvalidCardCompanies:
the if statements need to be fixed:
if (invalidCred[y[0]] === 3) {
should be
if (invalidCred[y][0] === 3) {
because you want to access the stuff at index y (by doing invalidCred[y] )
and then you want the first thing in there, so [0]

Also, you have too many returns there:
return companiesArray.push('Amex')
should just be
companiesArray.push('Amex');
because you want to add Amex to the array and keep going with the for loop (and the rest of the function)
(I also think that .push does not return anything useful.)

Its roughly the same problem for most of the other else-if statements in that loop too.

Also, use the </> button, and then paste the code in there to keep the formatting (the spacing and stuff) of the code in these posts

I made those fixes the code still seems to be missing something, is there something that should replace the .push?

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// 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:

function validateCred (array){
    let z = 0

  do {
   array = array[z] *2;
   if (array[z] > 9){
     array[z] = array[z]-9;
   }
   z = z + 2
  } while (z < array.length)

  let sum = 0;
for (let i = 0; i <= array.length; i++){
  sum = sum + array[i];
}
if (sum % 10 === 0){
 return true;
} else { return false; }
  
}

//loop for all arrays, then with calling, if false, add to another array and return it

function findInvalidCards(arrays) {
  let invalid = []
  for (let t = 0; t <= arrays.length; t++){
    if (validateCred.arrays[t] === false) {
   invalid.push(arrays[t]);
    }
  }
return invalid;
}

//1 loop through invalid card numbers, 2 for each use if statment to check first number and add to array what it is

function idInvalidCardCompanies (invalidCred) {
  let companiesArray = []
  for (let y = 0; y >= invalidCred.length; y++) {
    if (invalidCred[y][0] === 3) {
       companiesArray.push('Amex')
    } else if (invalidCred[y][0] === 4) {
       companiesArray.push('Visa')
    } else if (invalidCred[y][0] === 5) {
       companiesArray.push('Mastercard')
    } else if (invalidCred[y][0] === 6) {
       companiesArray.push('Discover')
    } else {
       companiesArray.push('Company not found')
    }
    
    return companiesArray.filter((value, index) => companiesArray.indexOf(value) === index);
  }
}

console.log(idInvalidCardCompanies([invalid2]))

console.log(idInvalidCardCompanies(batch))
//BOTH RETURN UNDEFINED INSTEAD OF MASTERCARD AND A LIST

One example of what could be improved:

when you write

console.log(idInvalidCardCompanies([invalid2]))

You’re invoking the function idInvalidCardCompanies() with [invalid2] as a parameter. Note that [invalid2] is an array within an array, since invalid2 is an array, and it’s within [].

You can and should put log statements to verify the inputs/returns and their types because that can really clarify where the debugging efforts need to be focused.

in the function idInvalidCardCompanies :
the for-loop has a problem:
for (let y = 0; y >= invalidCred.length; y++) {

The for-loop condition
y >= invalidCred.length
should be
y < invalidCred.length
in order for that loop to run

1 Like

that did it! thank you guys for helping me, its my first time using the forums and it was awesome