Credit Card Checker Challenge Project (JavaScript)

1 Like

Here’s my attempt!

My solution to the idInvalidCardCompanies() is different from the sample solution as I have used an object to store the details of the companies, and used forEach() to iterate over the object and check for duplicates.

1 Like

Hey everyone,

I found this project quite challenging at points but managed to get through it eventually! Check out my code here and let me know if you can see anywhere for improvements!

Thanks!

this is my code for the credit card project

So my first function can def be shortened up and I could have combined some of the lines but I did struggle in figuring it out the way I saw it in my head. Once I got it I did not go back through and combined some of the lines but I did find that much easier to read through as some of the steps are simply laid out. I did cruise past the remaining functions and I did add in another function to show the valid credit cards. I also created another function which allows for the display of card companies that have valid cards as well.

// 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:
// This fuction will find if the card is valid
function validateCred(array) {
  let newarr = []
  let evenNumArr = [];
  let oddNumArr = [];
  for ( i = (array.length - 1); i >= 0; i--){
    newarr.push(array[i]);
  }
  for (i = 0; i < newarr.length; i++){
    if (!(i % 2)) {
      evenNumArr.push(newarr[i])
    } 
  }
  for (i = 0; i < newarr.length; i++){
    if (i % 2) {
      let num = newarr[i] + newarr[i];
      if (num > 9) {
        oddNumArr.push(num - 9);
      } else {
      oddNumArr.push(num)
    } 
    }
}
let evenNum = evenNumArr.reduce((partial_sum, a) => partial_sum + a, 0);
let oddNum = oddNumArr.reduce((partial_sum, a) => partial_sum + a, 0);
let total = evenNum + oddNum;
let sum = (total % 10);
if (sum === 0) {
  return true;
} else {
  return false;
}
}
// Test to show if finding the valid card works
console.log(validateCred(valid4));

// This funciton uses the validate Credit Card function to find invalid cards 
// within an array of card numbers
function findInvalidCards(array) {
  invalCardsArray = [];
  for (j = 0; j < array.length; j ++) {
    if(validateCred(array[j]) === false) {
      invalCardsArray.push(array[j]);
    }
  }
  return invalCardsArray;
}
// Test to show the funciton is working
console.log(findInvalidCards(batch));

// This function finds the valid cards within an array of card numbers
function findValidCards(array) {
  valCardsArray = [];
  for (j = 0; j < array.length; j ++) {
    if(validateCred(array[j]) === true) {
      valCardsArray.push(array[j]);
    }
  }
  return valCardsArray;
}
// Test to show function is working
console.log(findValidCards(batch));

// This function creates an array of company names for invalid cards
function idInvalidCardCompanies(array) {
  invalidCardCompanies = [];
  for (k = 0; k < array.length; k ++) {
    if (array[k][0] === 3) {
      invalidCardCompanies.push('Amex');
    } else if (array[k][0] === 4) {
      invalidCardCompanies.push('Visa');
    } else if (array[k][0] === 5) {
      invalidCardCompanies.push('Mastercard');
    } else if (array[k][0] === 6) {
      invalidCardCompanies.push('Discover');
    }
  }
  return invalidCardCompanies;
}
// creating variable of invalid cards using function above
const invalidCards = findInvalidCards(batch);
// testing function
console.log(idInvalidCardCompanies(invalidCards))

// This function creates an array of company names for valid cards
function idValidCardCompanies(array) {
  ValidCardCompanies = [];
  for (k = 0; k < array.length; k ++) {
    if (array[k][0] === 3) {
      ValidCardCompanies.push('Amex');
    } else if (array[k][0] === 4) {
      ValidCardCompanies.push('Visa');
    } else if (array[k][0] === 5) {
      ValidCardCompanies.push('Mastercard');
    } else if (array[k][0] === 6) {
      ValidCardCompanies.push('Discover');
    }
  }
  return ValidCardCompanies;
}
// creating variable of valid cards using function above
const validCards = findValidCards(batch);
// testing function
console.log(idValidCardCompanies(validCards));

Here is my solution!

https://gist.github.com/bdc1702dabd5a935f29a2ef96e74d469

Hii you all,
this is my soulution for this task.
I would love to receive a code review.

I also spent ages trying to understand how that line works, in the end I think managed to get it, using a couple of console.logs() after each step.
I will try to explain in my own words what the loop does each time it runs, please correct me if I’m wrong:
will use this array as example: 4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8
1st time, currValue = 8:
(array.length - 1 - i) % 2 === 1)
array.length - 1 = 15, i = 15, % 2 will then be 0, so the first digit(the check digit) will not get multiplied and will be added to the “total” variable
total = 8

2nd time, currValue = 0:
(array.length - 1 - i) % 2 === 1)
array.length - 1 = 15, i = 14, % 2 will be 1, so will get multiplied by 2( the value to multiply is 0 so total will remain 8 as above)
total = 8

3rd time, currValue = 8:
(array.length - 1 - i) % 2 === 1)
array.length - 1 = 15, i = 13, % 2 will be 0, not getting multiplied, and added to total sum.
total = 16

4th time, currValue = 6:
(array.length - 1 - i) % 2 === 1)
array.length - 1 = 15, i = 12, % 2 will be 1, getting multiplied(12) and because is > 9 → will minus 9, result is 3, adding to total.
total = 19.

and so on…

hopefully this will help someone else that needs to learn at a slower pace, good luck.

findInvalidCards returns an array of invalid credit card number arrays I can’t recognize as part of the original array batch. The are two implementations of the same function, one is comented. Both seems to work the exact same way.

I really don’t know what is happening here, my thanks in advance to anyone who helps me

// 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 validCred (array) {
  let newArray = array;
  let sum = newArray.pop();
  newArray = array.reverse();
  
  for (i = 0; i < array.length; i++) {
    if (i % 2 === 0) {
        newArray[i] = newArray[i] * 2;
      if (newArray[i] > 9) {
        newArray[i] = newArray[i] - 9;
      }
    } 
    sum += newArray[i];
  }
  return sum % 10 === 0 ? true : false;
}


function findInvalidCards (nestedArray) {
  return nestedArray.filter(array => validCred(array) === false);
}

/*
function findInvalidCards (nestedArray) {
  const invalidCards = [];
  
  for (let i = 0; i < nestedArray.length; i++) {
    if (validCred(nestedArray[i]) === false) {
      invalidCards.push(nestedArray[i]);
    }
  }

  return invalidCards;

} 
*/

console.log(findInvalidCards(batch));

Hi,
somewhere in the project instructions it says, that you should make a copy of the array within the validCred function before you apply methods that alter the original array. .pop() alters the original array.

This:

does not make a copy of the array. It just creates another variable that points to the space the original array is allocated to.
You can make a copy by either using a method like .slice() or the spread syntax ([...originalArray])

Sorry, but the " set up on GitHub" link does not work.

I think i tshould be redirecting to this address though: https://www.codecademy.com/articles/f1-u3-git-setup

Thank you so much! Now the function returns the expected values

1 Like

It’s exciting to see many solutions from passionate learners.

Here’s my verbose solution with many enclosed explanations along the way.
:link: thepragdev-credit-card-checker-gist

Happy coding, fellas!

Hi Gabi,

I took a look at your code and it works perfectly fine. I would take a look at line 43 and think about what is being done there. Is it necessary?

Good job on line 53. Inverting the boolean return from your function with a logical not !.

calling idInvalidCardCompanies(batch); worked as expected. The switch statement works well in this case. I like how you used it to get the string array. My only concern would be that the switch statement is written once and used once. I wonder if there is a way to reuse this functionality over and over again to determine a card brand?

Good work.

Hi Cliffy

The resone i use the reverse method on line 43 is beacuse when we call a function and give an array
the array is forword to the function by refernce and if i revers one time i need to revers it again.

When i post my solution i forgat to add this line console.log(idInvalidCardCompanies(findInvalidCards(batch)));
There is a way to reuse this functionality for this we need to change the idInvalidCardCompanies a littiel bit
insted of provide an array of arrays to this function we only provide a single array each time and only check what is the first number of the array and return the type by using the switch statement

Thanks for the honst review!

1 Like

I just finished my credit card checker program and I wanted to share it. It isn’t the most pretty but it gets the job done.

Hello!
This is my solution :grinning: