Credit Card Checker Challenge Project (JavaScript)

Currently stuck on part 4 of the exercise. When I try to run this function using the batch provided, I keep getting an empty array. Not sure what I am missing.

let invalidCardList = ;
const findInvalidCards = (arr) => {
for (i = 0; i < arr.length; i++) {
if (validateCred(arr[i]) === false) {
invalidCardList.push(arr[i]);
}
}
return invalidCardList;
}

console.log(findInvalidCards(batch)); // currently returns

check whether the validateCred function returns true or false or something else.

The results are not very different, but I like them.
:slightly_smiling_face: :slightly_smiling_face: :slightly_smiling_face:

Challenge Project: Credit Card Checker

Hi all. I am getting a repeated return for the CC companies in the last function. I used switch first but changed to if/else since I always have trouble with those statements (I dont know why). Code and output below:

Output:
[ ‘Visa’,
‘Mastercard’,
‘Amex (American Express)’,
‘Discover’,
‘Mastercard’,
‘Amex (American Express)’,
‘Discover’,
‘Visa’
]

Code:
const idInvalidCardCompanies = (invalidCompany) => {

let cardCompany = ; // empty array for companies

let invalidCards = findInvalidCards(invalidCompany); // find invalid cards to check

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

let firstDigit = invalidCards[i][0]; // get first digit of invalid card numbers

if (firstDigit === 3) {

  cardCompany.push("Amex (American Express)");

} else if (firstDigit === 4) {

  cardCompany.push("Visa");

} else if (firstDigit === 5) {

  cardCompany.push("Mastercard");

} else if (firstDigit === 6) {

  cardCompany.push("Discover");

} else {

  console.log("Company Not Found");

}

}

return cardCompany;

};

console.log(idInvalidCardCompanies(batch));

you’d could check whether something’s not in the array yet, before you add it to the array:

for example:

if (firstDigit === 3 && !cardCompany.includes("Amex (American Express)") ) {
  cardCompany.push("Amex (American Express)");
}

I had thought about that being a possibility before I submitted the question but never did anything about it. Thanks for the help!

Hi to everyone! And this is my solution of the problem.

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

// Function accept arr. Checking card number and return ‘true’ or ‘false’.

function validateCred(arr){
arr.reverse();//reversing arr
let sum = 0;
for (let i = 0; i < arr.length; i++){
if(i % 2 === 0){
sum += arr[i];
}else{
if(arr[i]*2 > 9){
sum += arr[i]*2 - 9;
}else{
sum += arr[i]*2;
}
}
}
arr.reverse();//reversing arr back
if (sum % 10 === 0){
return true;
}else{
return false;
}
};

/Function taking nested array of credit card numbers. Logging invalid cards and return array of invalid cards/

function findInvalidCards(arr){
let invalid = ;
for (let elem of arr){
if(!validateCred(elem)){
invalid.push(elem);
console.log(Card #: ${elem.join('')} ...is invalid);
}
}
return invalid;
}

/Function checking companies which provide incorrect cards/

function idInvalidCardCompanies(arr){
let listOfCompanies = ;
//Iterating through arr of invalid cards
for(let elem of arr){
if (elem[0] === 3){
//If arr includes that name - it will not be added anymore
if(!listOfCompanies.includes(‘Amex’)){
listOfCompanies.push(‘Amex’);
}
}else if (elem[0] === 4){
if(!listOfCompanies.includes(‘Visa’)){
listOfCompanies.push(‘Visa’);
}
}else if (elem[0] === 5){
if(!listOfCompanies.includes(‘Mastercard’)){
listOfCompanies.push(‘Mastercard’);
}
}else if (elem[0] === 6){
if(!listOfCompanies.includes(‘Discover’)){
listOfCompanies.push(‘Discover’);
}
}else{
listOfCompanies.push(‘Company not found’);
}
}
console.log(‘Companies which produce incorrect cards:’)
return listOfCompanies;
}

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

//TASK 7 - Function transform string into array of digits
function stringToArr(string){
let arr = ;
for(let char of string){
arr.push(+char);
}
return arr;
}

//TASK 7 (2) Function that will convert invalid numbers into valid numbers.
function invalidToValid(arr){
arr.reverse();
let sum = 0;
for (let i = 0; i < arr.length; i++){
if(i % 2 === 0){
sum += arr[i];
}else{
if(arr[i]*2 > 9){
sum += arr[i]*2 - 9;
}else{
sum += arr[i]*2;
}
}
}
if (arr[0]>=sum%10){
arr[0] -= sum%10;
}else{
arr[0] += (10-sum%10);
}
return arr.reverse()
}
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-22-javascript-syntax-part-ii/modules/wdcp-22-credit-card-checker/projects/credit-card-checker

All you need is just change condition in “if…else” statetment. If condition true - code block is working if not - it just skip it. Since we need add arr when it false we using "!’ operator. Hope it will help you.

let invalidCardList = ;
const findInvalidCards = (arr) => {
for (i = 0; i < arr.length; i++) {
if (!validateCred(arr[i]) {
invalidCardList.push(arr[i]);
}
}
return invalidCardList;
}

i had similar issue but i added if(cardCompany.includes("Amex") continue; etc.

hi everyone, just finished the challenge, here’s my version:

https://github.com/chylinski82/credit_card_checker

Tried changing the if statement like you showed and I am still getting an empty array when I try running my invalidCardList function with the batch array provided. Maybe there is something wrong with my .push method since it looks like the invalid arrays aren’t being added to invalidCardList. Either that or I am missing something.

don’t forget to declare i as let i = 0 :

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

Here is my solution, somehow I managed to make it shorter that the provided solution (I feel a bit proud about that).
ps. the line that filters the duplicates (the last one in the function), I have no idea why it works. I just found it online and copied it.

janbazant1107978602 is right. I forget to declarate various ‘i’ with let. Just add declare i with let and it should work.

janbazant1107978602 is right. I forget to declarate various ‘i’ with let. Just add declare i with let and it should work.

That worked. I have to remember to declare the i using let. Sometimes it’s easy to forget to do so.

I come from a Ruby background, so I really like to use methods instead of for loops when I can.

Here’s how I did that with this project. I’m happy to hear feedback.

My solution:

// 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]
const valid6 = [4, 5, 3, 9, 6, 8, 9, 8, 8, 7, 7, 0, 5, 7, 9, 8]

// 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:
const validateCred = ( card ) => {

    card = Array.isArray( card )
        ? card
        : Array.from( card, Number );

    return card.reverse()
            .reduce( ( acc, curr, index ) => {
                if ( index % 2 === 0 ) {
                    return acc + curr;
                } else {
                    let num = curr * 2;
                    
                    if ( num > 9 ) {
                        num -= 9;
                    }

                    return acc + num;
                }
            }, 0 ) % 10 === 0;

}

const findInvalidCards = ( cards ) => {
    return cards.filter( ( card ) => !validateCred( card ) );
}

const idInvalidCardCompanies = ( invalidCards ) => {

    let companies = {
        3: 'Amex',
        4: 'Visa',
        5: 'Mastercard',
        6: 'Discover'
    };
    
    return [ ...new Set( invalidCards.map( ( card ) => {
        return companies[ card[ 0 ] ] || 'Company not found';
    } ) ) ];
    
}

const invalidCards = findInvalidCards( batch );

console.log( idInvalidCardCompanies( invalidCards ) );

1 Like

hi all,
junior here, :wave:t2:
i just finished the project, if a pro mate check my solution and tell me how can i accept large numbers for input, currently i found my code output did not work correct with large card numbers like ‘6011686132019343139’.

GitHub
Source:

// 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:
const validateCred = (cardNumber) => {
  //console.log(cardNumber); //debug log
  let sumChecker = 0;
  for (let i = cardNumber.length - 1; i >= 0; i--) {
    sumChecker = sumChecker + cardNumber[i];
    //console.log(sumChecker); //debug log
    i--;
    if (i >= 0) {
      let doubled = cardNumber[i] * 2;
      if (doubled >= 10) {
        sumChecker = sumChecker + (doubled - 9);
        //console.log(sumChecker); //debug log
      } else {
        sumChecker = sumChecker + doubled;
        //console.log(sumChecker); //debug log
      }
    }
  }
  if (sumChecker % 10 === 0) {
    console.log(
      "Card is Valid. card number: " + cardNumber.join("") + " //Valid"
    ); //debug log
    return true;
  } else {
    console.log(
      "Card is not Valid, check and try again. " +
        "card number: " +
        cardNumber.join("") +
        " //Invalid"
    ); //debug log
    return false;
  }
};

const findInvalidCards = (cards) => {
  let invalidCards = [];
  for (let card of cards) {
    if (!validateCred(card)) {
      invalidCards.push(card);
    }
  }
  return invalidCards;
};

const idInvalidCardCompanies = (invalids) => {
  let companies = [];
  for (let card of invalids.sort()) {
    switch (card[0]) {
      case 3:
        if (companies.indexOf("Amex") === -1) {
          companies.push("Amex", card);
        } else companies.push(card);
        break;
      case 4:
        if (companies.indexOf("Visa") === -1) {
          companies.push("Visa", card);
        } else companies.push(card);
        break;
      case 5:
        if (companies.indexOf("Mastercard") === -1) {
          companies.push("Mastercard", card);
        } else companies.push(card);
        break;
      case 6:
        if (companies.indexOf("Discover") === -1) {
          companies.push("Discover", card);
        } else companies.push(card);
        break;
      default:
        if (companies.indexOf("Company not found") === -1) {
          companies.push("Company not found", card);
        } else companies.push(card);
        break;
    }
  }
  return companies;
};

const checkMyCard = (cardNumber) => {
  let toNum = (num) => Number(num);
  let arr = Array.from(String(cardNumber), toNum);
  return validateCred(arr);
};

//console.log(idInvalidCardCompanies(findInvalidCards(batch))); //Batch Checker

checkMyCard(4844051398444670); //singel card checker