Credit Card Checker Challenge Project (JavaScript)

Hi, the following is my code :slight_smile:

// 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: //validateCred() checks a credit number valid or not. const validateCred = (arr) => { const newarr = [...arr]; newarr.reverse(); for (let i = 1; i < newarr.length; i += 2) { newarr[i] = newarr[i] * 2; if (newarr[i] > 9) { newarr[i] -= 9; } } let sum = 0; for (let i = 0; i < newarr.length; i++) { sum += newarr[i]; } if (sum % 10 === 0) { return true; } else { return false; } }; //findInvalidCards() outputs an array of invalid card numbers. const findInvalidCards = (nestedarr) => { let invalidList = []; for (let i = 0; i < nestedarr.length; i++) { if (validateCred(nestedarr[i]) === false) { invalidList.push(nestedarr[i]); } } return invalidList; }; //idInvalidCardCompanies() outputs a list of companies that issue invalid card numbers in the above invalid list. const idInvalidCardCompanies = (nestedarr) => { let firstDigits = []; for (let i = 0; i < nestedarr.length; i++) { firstDigits.push(nestedarr[i][0]); } console.log(`There are ${firstDigits.length} invalid card numbers. `); console.log("First digits of the found invalid card numbers: " + firstDigits); let companyList = []; if (firstDigits.some((digit) => digit === 3)) { companyList.push("Amex (American Express)"); } if (firstDigits.some((digit) => digit === 4)) { companyList.push("Visa"); } if (firstDigits.some((digit) => digit === 5)) { companyList.push("Mastercard"); } if (firstDigits.some((digit) => digit === 6)) { companyList.push("Discover"); } // the following if statement: .filter() attempts to find the numbers either greater than 6 or less than 3, if there is any, 'an unnamed company' will be added to the company list. let notdigit3to6 = firstDigits.filter((digit) => digit < 3 || digit > 6); if (notdigit3to6.length !== 0) { companyList.push("an unnamed company"); } console.log( "List of Companies that issue invalid numbers: " + companyList.join(", ") ); }; //test these functions! findInvalidCards(batch); idInvalidCardCompanies(findInvalidCards(batch));

Here is my solution to the credit card checker exercise. I feel I’ve manged to create some efficient code that does the job. Plenty of console.log throughout the code which really helped me see what was happening and if it was what I was expecting. Good luck and message if you have any questions

This was a bit of a learning curve going from the guided exercises to ‘figure it out’. I may have wanted to throw my computer out the window a few times… but it really helped solidify the lessons by going through the research and the trial and error: Check CC Numbers To Identify Invalid Issued Cards · GitHub

Hi there, Im new into this world and here’s my code for the project. I know it can be optimized, but it works. Any suggestions are welcome!
Giuliano

Well here is my attempt. It is not the solution but I am hoping it is the correct output. I am getting a list of invalid companies.

This challenge took me a while to figure out, but here is my solution :

Feedback is welcome :slight_smile:

Credit Card checker

Happy coding!!

Here’s my solution for this challenge

Here is my solution to the challenge. Quite a good bit I’ve learned. I think making my code a bit more readable while also trying to cut down on the variables I use would help!

https://github.com/andreweunson/validatingCardNumbers/blob/main/main.js

Hi friends, please find my solution to the challenge. All forms of comments are welcome.

My solution:

Hi everyone, wanted to share my solution here: Codecademy export · GitHub

let me know your thoughts!

Hi Everyone
This is my Solution:

Hello friends, here is my solution, I hope you can spare some time and tell me how it looks. Open for any critique and advises.

By the way, i found out that my first function finds the valid3 array, which is supposed to be valid, invalid. I realized it is because there are only 15 digits instead of 16. Did you also face the same problem? Did you solve it or decided to ignore?

I guess you could do different stuff depending on whether the length of the array is even or odd.
like

“if the array has an even length and the index is an even number, then double the number at that index and do stuff to it”
or
“if the array has an odd length and the index is not an even number, then double the number at that index and do stuff to it”

function validateCred(arr) {
  let newArr = arr.slice();
  for (let i = newArr.length - 1; i >= 0; i--) {
    if ((arr.length % 2 === 0 && i % 2 === 0) || (arr.length % 2 !== 0 && i % 2 !== 0)) {
      newArr[i] *= 2;

I didn’t use the index to do that stuff in mine … I used a variable to count how far the index was from the end [or length] instead.

Hello. Here is My solution to the CreditCardChecker JavaScript Project. I would be thankful to any feedback given. Good luck to everyone and Happy Coding!

It was a really good idea. Thank you!

I still don’t get it.
switch (invalidBatch[i][0]) {
case 3:
if (companies.indexOf(‘Amex’) === -1) {
companies.push(‘Amex’);
}

Here is my solution. I believe I kept the solution quite short and concise. It took quite a while to figure a couple of problems out. It surprised me for a problem that initially seemed quite simple at first glance!

I thought I had the validation correct until I tested it and realised I needed to reverse the array first because credit cards can be different lengths so the modulo function only lined up with the correct indices on even numbered length cards.

I had quite a headache with the function to gather a list of invalid companies.

I really enjoyed the project, it made me really think to utilise the function’s I’ve learned in a way that led me to a functioning solution.

// Add your functions below:
function validateCred(arr) {
  arr2 = arr.slice().reverse() // reverse the array
  let total = arr2[0] // first digit remains the same

  for (i=1; i < arr2.length; i++) {
    const d = arr2[i] // get card digit

    if (i % 2 === 0) {  // if card digit index is even
      total += d
    } else {  // if card digit index is odd
      total += d*2 > 9 ? d*2-9 : d*2   
    }
  }
  return total % 10 === 0   // is card valid?
}

function findInvalidCards(arr) {
  result = []
  for(card of arr) {  // loop through every card in array
    if (validateCred(card)) {
      result.push(card) // add card to array if valid
    }
  }
  return result
}

function idInvalidCompanies(arr) {
  // a list of companies by card first digit.
  const companies = [
    {_firstDigit: 3, _company: 'Amex (American Express)'},
    {_firstDigit: 4, _company: 'Visa'},
    {_firstDigit: 5, _company: 'Mastercard'},
    {_firstDigit: 6, _company: 'Discover'},
  ]
  // an array of the first digits from companies array
  const firstDigits = companies.map(company => company._firstDigit)
  const invalidCompanies = []

  // loop through each credit card in arr
  for (let card of arr) {
    const firstDigit = card[0] // the first digit of the card
    const matchingIndex = firstDigits.indexOf(firstDigit) // get the matching index in companies array

    if (matchingIndex === -1) {
      console.log('Company not found') // no match
    } else {
      if(!invalidCompanies.includes(companies[matchingIndex]._company)) {
        invalidCompanies.push(companies[matchingIndex]._company) // add to invalidCompanies array if it isn't already contained inside.
      }
    }
  }
  return invalidCompanies
}

Hello, really fun project. Challenging and great way to learn JavaScript.
Here is the code on Github - https://github.com/QuicoCat/JavascriptCodecademy/blob/main/crediccardchecker.js

// 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 = arr => {
  let sumOfNewArr;
  let sumModulo; 
  let newArr = [];

  //Function to perform the algorithm if the array has an odd number length

  const luhnAlgorithmOdd = arr => {
    for (let i = arr.length - 1; i >= 0; i--) {
        if (i === arr.length - 1 || i%2 === 0 || arr[i] === 0) {
          newArr.push(arr[i]);
        } else if (i%2 !== 0 && arr[i] * 2 < 10) {
          newArr.push(arr[i] * 2);
        } else if (i%2 !== 0 && arr[i] * 2 > 9) {
          newArr.push((arr[i] * 2) - 9);
        }
      }
    }

  //Function to perform the algorithm if the array has an even number length

    const luhnAlgorithmEven = arr => {
    for (let i = arr.length - 1; i >= 0; i--)  {
        if (i === arr.length - 1 || i%2 !== 0 || arr[i] === 0) {
          newArr.push(arr[i]);
        } else if (i%2 === 0 && arr[i] * 2 < 10) {
          newArr.push(arr[i] * 2);
        } else if (i%2 === 0 && arr[i] * 2 > 9) {
          newArr.push((arr[i] * 2) - 9);
        }
      }
    }

  // Determine if the array has an odd or even .length property, then call the  correct function

    if ((arr.length - 1)%2 !== 0) {
      luhnAlgorithmEven(arr);
    } else {
      luhnAlgorithmOdd(arr);
    }

  sumOfNewArr = newArr.reduce((x,y) => x + y); // Sums all numbers in the array after performing the algorithm
  sumModulo = sumOfNewArr%10; // Remainder of sum of all numbers divided by 10

  // Determine the validity of the array based on the sum modulo

  if (sumModulo === 0) {
    return 'valid';
  } else return 'invalid';
}

const findInvalidCards = nestedArr => {
  let invalidCards = []; // Array to place the invalid card arrays.
  let invalidCard = [];
  let arrValidity;

  //Cycles through each array in the nested array to find the invalid ones using the validateCred function
  for (let i = 0; i < nestedArr.length; i++) {
    invalidCard = nestedArr[i];
    arrValidity = validateCred(invalidCard);
    if (arrValidity === 'invalid') {
      invalidCards.push(invalidCard);
    }
  }
  return invalidCards;
}

const idInvalidCardCompanies = nestedArrInvNumb => {
  let firstDigit;
  let companies = [];

  //To cycle through each first digit of the arrays
  for (let i = 0; i < nestedArrInvNumb.length; i++) {
    firstDigit = nestedArrInvNumb[i][0];
    
    // To check each firstDigit and not include the company name if it has already been added to the array
    if (firstDigit === 3 && companies.includes('Amex') === false) {
      companies.push('Amex');
    } else if (firstDigit === 4 && companies.includes('Visa') === false) {
      companies.push('Visa')
    } else if (firstDigit === 5 && companies.includes('Mastercard') === false) {
      companies.push('Mastercard')
    } else if (firstDigit === 6 && companies.includes('Discover') === false) {
      companies.push('Discover')
    }
  }
  return companies;
}

Code review please if anyone is available.