Credit Card Checker Challenge Project (JavaScript)

1 Like

Hello everyone!
I just finalized my Credit Card Checker project. I’ve used everything in my mental toolbox that I’ve put together with Codecademy thus far. Please let me know if there is anything I can do to improve this code. Thanks!
Credit Card Checker repository on GitHub

1 Like

Hi here is my solution:

1 Like

hey @markamarzan nice one
i saw your solution.
Its kinda same with me.
Do check my solution in github at here: GitHub - amanc1248/creditCardValidater
However if you want to go along with the full stack journey do let me know.
Find me at linkedin: https://www.linkedin.com/in/aman-chaudhary-4a8725173/

1 Like

I really enjoyed this project (after banging my head on the desk a few times after I got stuck), because I got introduced to Set.
Check it out at step 5 of the code:

const validateCred = function (arr){
  let a = [ ];
  let b = [ ];
  let newArr = [ ];
  //for loop to seperate array in two arrays, one (a) for *2 and one(b) for *1
  for(let i = 0; i < arr.length; i++){
    if(i % 2 === 0){
      a.push(arr[i]);
    } else {
      b.push(arr[i]);
    };
  };
  //verified a and b have the correct data in the array
  for(let k = 0; k < a.length; k++){
    a[k] = a[k] * 2;
  };
  //verified a now contains each element * 2
  for(let j in a){
    if(a[j] >= 10){
      a[j] = a[j] - 9;
    } else {
      a[j] = a[j];
    };
  };
  //verified array a has every element > 9 subtracted by 9
  newArr = newArr.concat(a, b);
  // verified newArr holds the value of arrays a and b
  arrSum = newArr.reduce((x, y) => x + y);
  // verified arrSum correctly adds all elements in newArr
  if(arrSum % 10 === 0){
    return true;
  } else {
    return false;
  };
  //verified validateCred properly returns true or false
};

//step 4

const findInvalidCards = function(arr){
    let c = [ ];
    for (let i = 0; i < arr.length; i++){
      if (validateCred(arr[i]) === false){
        c.push(arr[i])
      };
     };
     return c;
};
//verified findInvalidCards returns invalid arrays

//step 5

const idInvalidCardCompanies = function(arr){
 let d = [ ];
    for (let i = 0; i < arr.length; i++){
      if (arr[i][0] === 3){
        d.push('Amex');
      } else if (arr[i][0] === 4){
        d.push('Visa');
      } else if (arr[i][0] === 5){
        d.push('Mastercard');
      } else if (arr[i][0] === 6){
        d.push('Discover');
      } else {
        d.push('Company not found')
      };
    };
  e = new Set(d);
  //uses Set to eliminate duplicates
  f = Array.from(e);
  //converts Set e to an array
  return f;
};
// verified  idInvalidCardCompanies() returns names using below variable 'invalid'
invalid = [[ 3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6 ],[ 4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5 ],[ 5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3 ],[ 3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4 ],[ 6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5 ],[ 5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4 ],[ 3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4 ],[ 4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3 ]];     
1 Like

Hey!

Well done on completing the project! When sharing code on the forums be sure to format it according to this guide: How do I format code in my posts?, it just makes it a lot easier to see/read the code. :slight_smile:

Happy coding!

2 Likes

Hi! I realized how useful the methods are.

const validateCred = arr => {
  let sum = 0;
  
  for(let i = arr.length - 1; i >= 0; i--){
    if(!(i%2===0)){
      sum += arr[i];
    }else{
      let doubled = arr[i]*2;
      if(doubled > 9){
        doubled -= 9;
        sum += doubled;
      }else{
        sum += doubled;
      }
    }
  }

  return sum % 10 === 0;
}



const findInvalidCards = arr => {
  let arr2 = arr.filter(element => !(validateCred(element)))
  return arr2;
}


const idInvalidCardCompanies = arr => {
  let arr2 = [];
  for(let i = 0; i < arr.length; i++){
    switch(arr[i][0]){
      case 3:
      arr2.push('Amex');
      break;
      case 4:
      arr2.push('Visa');
      break;
      case 5:
      arr2.push('Mastercard');
      break;
      case 6:
      arr2.push('Discover');
      break;
      default:
      console.log('Company not found');
    }
  }

  let arr3 = arr2.filter((item,index)=>{
      return arr2.indexOf(item) === index; 
    })

  return arr3;
}
1 Like

Hi everyone!
In my code, I tried to use Iterator functions (.filter(), some(), map(), reduce()):

1 Like

Hey guys here’s my solution to this code challenge

I know it’s not the most efficient way if doing it but I managed to figure it out on my own which I’m happy with.

3 Likes

My code is given below. You can also check out my GitHub: creditCardChecker/creditCardChecker.js at main · ahman502/creditCardChecker · GitHub

// 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]; const mystery6 = [0, 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, mystery6]; // first function to find valid credit cards function validateCred(arr) { let minusNine; let timesTwo; let sumOne = 0; let sumTwo = 0; console.log(`Given array: [${arr}]`); console.log(`-------- Calculating sumOne --------`); for(let i = arr.length - 2; i >= 0; i--) { console.log(`\nNumber in array = ${arr[i]}`); timesTwo = arr[i] * 2; console.log(`${arr[i]} x 2 = ${timesTwo}`); if(timesTwo > 9) { console.log(`${timesTwo} > 9 so we subtract 9`); minusNine = timesTwo - 9; console.log(`${timesTwo} - 9 = ${minusNine}`); console.log(`${sumOne} from last step + ${minusNine} from this step`); sumOne += minusNine; } else { console.log(`${sumOne} from last step + ${timesTwo} from this step`); sumOne += timesTwo; } console.log(`Total sumOne = ${sumOne}`); i--; } console.log(`\n-------- Calculating sumTwo --------`); for(let i = arr.length - 1; i >= 0; i--) { console.log(`\nNumber in array = ${arr[i]}`); console.log(`${sumTwo} from last step + ${arr[i]} from this step`); sumTwo += arr[i]; console.log(`Total sumTwo = ${sumTwo}`); i--; } console.log(`\nFinal sumOne = ${sumOne}`); console.log(`Final sumTwo = ${sumTwo}`); console.log(`Final TOTAL sum = ${sumOne} + ${sumTwo} = ${sumOne + sumTwo}`); if((sumOne + sumTwo) % 10 == 0) { console.log(`${sumOne + sumTwo} % 10 = 0`); return true; } else { console.log(`${sumOne + sumTwo} % 10 != 0`); return false; } } // second function to find all invalid credit cards and return an array that holds all invalid credit card numbers let invalidCards = []; function findInvalidCards(arr) { for(let i = 0; i < arr.length; i++) { if(!validateCred(arr[i])) { console.log('Array above is NOT valid\n'); invalidCards.push(arr[i]); } else { console.log('Array above is valid\n'); } } console.log('An array of ALL invalid arrays:') console.log(invalidCards); return; } // third function to identify the credit card companies that have possibly issued the faulty numbers function idInvalidCardCompanies(arr) { console.log('Credit card companies that have possibly issued these faulty numbers:') let amexCounter = 0; let visaCounter = 0; let masterCardCounter = 0; let discoverCounter = 0; let companiesArr = []; for(let i = 0; i < arr.length; i++) { if(arr[i][0] == 3) { while(amexCounter < 1) { companiesArr.push('Amex'); amexCounter++; } } else if(arr[i][0] == 4) { while(visaCounter < 1) { companiesArr.push('Visa'); visaCounter++; } } else if(arr[i][0] == 5) { while(masterCardCounter < 1) { companiesArr.push('Mastercard'); masterCardCounter++; } } else if(arr[i][0] == 6) { while(discoverCounter < 1) { companiesArr.push('Discover'); discoverCounter++; } } else { companiesArr.push('Company not found'); } } console.log(companiesArr); return; } // Testing all functions below console.log('********* Testing first function *********\n'); console.log(validateCred(valid1)); //console.log(validateCred(invalid2)); //console.log(validateCred(valid5)); //console.log(validateCred(mystery1)); console.log('\n********* Testing second function *********'); findInvalidCards(batch); console.log('\n********* Testing third function *********\n'); idInvalidCardCompanies(invalidCards);

Please run the program above to see the output. My output is very long and extensive so I couldn’t copy-paste it. I did this problem the long way but I understood exactly what I was doing so I am happy with this solution. However, I would also love your feedback or any suggestions to improve this code. Thank you!

2 Likes

Here is my Credit Card Checker Challenge Project.
GitHub - GitHub - rajevesubnaik/Credit-Card-Checker-Project: check whether a credit card is valid or invalid

// 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:
// determin if card number is valid or invalid
const validateCred = credArray => {
    // make copy of origninal array so we don't modify
    const copyArray = [...credArray];
    // reverse the array
    const reverseArray = copyArray.reverse();
    // remove to check digit
    const checkDigit = reverseArray.shift();
    // loop through every other digit in array and double. If double is > 9 then subtract 9
    const everyOther = [];
    for (let i = 0; i < reverseArray.length; i++) {
        if (i % 2 == 0 ) {
            const double = reverseArray[i] * 2;
            if (double > 9) {
                const notDouble = double - 9;
                everyOther.push(notDouble);
            } else {
                everyOther.push(double);
            }
        } else {
            everyOther.push(reverseArray[i]);
        }
    }
    // add back the check digit
    everyOther.unshift(checkDigit);
    // find the sum of the array
    const sum = everyOther.reduce((prevVal, currentVal) => prevVal + currentVal);
    // determine if the card is valid or invalid
    const isValid = sum / 10 % 2 === 0 ? true : false;
    return isValid;
}

// return invalid card numbers
const findInvalidCards = credCard => {
    const invalidCard = [];
    // loop through an array of card numbers and determine ones that are invalid
    for (let i = 0; i < credCard.length; i++) {
        if (!validateCred(credCard[i])) {
            invalidCard.push(credCard[i]);
        }
    }
    return invalidCard;
}

// determine which card company issued invalid cards
const idInvalidCardCompanies = invalidCard => {
    const cardCompanies = [];
    for (let i = 0; i < invalidCard.length; i++) {
        switch (invalidCard[i][0]) {
            case 3: 
                cardCompanies.push('amex');
                break;
            case 4: 
                cardCompanies.push('visa');
                break;
            case 5: 
                cardCompanies.push('mastercard');
                break;
            case 6: 
                cardCompanies.push('discover');
                break;
            default: 
                console.log('Company not found');
                break;
        }
    }
    // remove duplicate card companies after iterating array
    const removeDuplicate = [... new Set(cardCompanies)];
    return removeDuplicate;
}

// convert a number string to an array
const numStringToArray = string => {
    return string.split('').map(Number);
}

console.log(findInvalidCards(batch));

console.log(idInvalidCardCompanies([invalid1, invalid2, invalid3, invalid4, invalid5]));

console.log(numStringToArray('4744677908016808'));

Here is my Credit Card Checker Challenge Project - check whether a credit card is valid or invalid

Hello to everyone!
Here is 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, 7, 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 allValids = [valid1, valid2, valid3, valid4, valid5]; // 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]; const allInvalids = [invalid1, invalid2, invalid3, invalid4, invalid5]; // 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]; const allMysteries = [mystery1, mystery2, mystery3, mystery4, mystery5]; // 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 = (arr) => { let sum = 0 for(let i = 0; i < arr.length; i++){ if (i % 2 === 0) { arr[i] >= 5? sum += arr[i] * 2 - 9: sum += arr[i] * 2; } else { sum += arr[i]; } } return sum % 10 === 0; } function findInvalidCards(array){ const invalidCards = []; array.forEach(function(card) { if(validateCred(card) === false) { invalidCards.push(card); } }) return invalidCards; } const idInvalidCardCompanies = array => { array = findInvalidCards(array); const companies = new Set(); array.forEach(card => { const firstDigit = card[0]; switch (firstDigit) { case 3: companies.add("Amex (American Express)"); break; case 4: companies.add("Visa"); break; case 5: companies.add("Mastercard"); break; case 6: companies.add("Discover"); break; default: companies.add("Company not found"); break; } }) return companies; } console.log(idInvalidCardCompanies(batch))

16 posts were split to a new topic: Credit Card Checker Problems

I think I’ve managed to complete this task, but I’d love to know what glaring improvements I could have made!

Hi everyone, I have completed this challenge. Would really appreciate any feedback, please. Thank you.

Here is my solution :
Credit Card Checker solution

Hello, this is my solution.

PS, if you are looking for motivation: i started the full-stack path a long time ago i always broke down whenever i couldn’t get a solution (mostly with JS II part) and dropped the path 2 times. Then i decided i should start over my journey of coding by taking de CS path from CC. Now, i finally get to publish my own solution for these type of challenges and i’m super happy :slight_smile: You’ll get there!

1 Like

I also noticed this. Maddening lol – Codecademy needs to address this