Credit Card Checker Challenge Project (JavaScript)

Hello everyone,
Here is my solution of the challenge - credit card checker:
https://gist.github.com/99e16f74a26fce2d9c8c68663788aaff

Hello Everyone, Please kindly check my solution on Credit Card Number Validator Project.
Thankyou.

Hi Guys,
Let me know -

// 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 = (credNumber) => {
  let sum = 0;
  for (let i = credNumber.length-1; i >= 0; i--){
    let n = credNumber[i];
    if(i % 2 === ((credNumber.length) % 2)){
      n *= 2;
      if (n > 9){
        n -= 9
      }
      sum += n;  
    }else{
      sum += n;
    }
  }
  return (sum % 10 === 0) ? 'Valid' : 'Invalid';
  };



const findInvalidCards = (nestArray) => {
  const invalidArrays = [];
  for (let i = 0; i < nestArray.length; i++) {
    if (validateCred(nestArray[i]) === "InValid") {
      invalidArrays.push(`${nestArray[i]}: InValid`);
    }
  }
  console.log(invalidArrays);
};

const idInvalidCardCompanies = (nestArray) => {
  const companyArray = [];
  for (let i = 0; i < nestArray.length; i++) {
    switch (nestArray[i][0]) {
      case 2:
        if (!companyArray.includes("Mastercard")) {
          companyArray.push("Mastercard");
        }
      case 3:
        if (!companyArray.includes("Amex (American Express)")) {
          companyArray.push("Amex (American Express)");
        }
        break;
      case 4:
        if (!companyArray.includes("Visa")) {
          companyArray.push("Visa");
        }
        break;
      case 5:
        if (!companyArray.includes("Mastercard")) {
          companyArray.push("Mastercard");
        }
        break;
      case 6:
        if (!companyArray.includes("Discover")) {
          companyArray.push("Discover");
        }
        break;
      default:
        if (!companyArray.includes("Company not found")) {
          console.log("Company not found");
        }
        break;
    }
  }
  console.log(companyArray);
};

const convertString = (string) => {
  const newArray = [];
  for (let i of string) {
    newArray.push(parseInt(i));
  }
  return newArray;
};

Thx
R

Here’s my submission. If you look at it and see something you’d like to point out, please let me know.

Found this tough, mainly the last function actually. It certainly didn’t take me 40mins which seems to be a frequent occurrence :S
my code

This is my solution:

Hey guys! Just finished this project, here’s my solution:

https://gist.github.com/446957e3f97f00213a35d4e68583910b

Is there any way to improve my code?

Any feedback is appreciated :smiley:

Hello Samjeed99,

I really like how you use the .filter to find the invalid cards. I like the way you use swicth/case, but I am not sure if I understood very well. Wondering why the index -1.

Thank you for sharing!

Hello Everyone!

Follow my code, please, I am open to any feedback. It will help me to improve:

I feel like I completed this in the most simplistic form with a complex breakdown. So in all terms, it’s more of a step-by-step basic code form completion rather than an immediate refactored form.

Take a look here at GitHub for the full code file: Invalid-Credit-Card-Checker/main.js at main · DEVArt1st/Invalid-Credit-Card-Checker · GitHub

Completed the task with the additional challenge of making a function that converts invalid cards into valid numbers. Used nested loops to work through the number backwards and edit the values until they satisfied the original validating function.

Initially had an issue with the conversion function overwriting the original invalid code, until I made the new array a .map of the invalid one rather than declaring newArr = arr at the start of the function.

Take a look! Feedback appreciated as always.

javascript/creditcardvalidator at main · JakeyBlee/javascript (github.com)

I´ve done it. It wasn´t easy but there was this strange feeling of satisfaction when I had it finished finally!

Here is my solution - a bit more verbose than the provided solution, but works and does things in a very clear step by step way.

Also found the use of the Set constructor very helpful for removing duplicate values from array.

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

// validateCred check if the given credit card number is valid.
const validateCred = (arr) => {
  let newArr = [];
  //loop through every number that need to multiply by 2 and apply the conditions then add to the new array.
  for (let i = arr.length - 2; i >= 0; i -= 2) {
    arr[i] * 2 > 9 ? newArr.push(arr[i] * 2 - 9) : newArr.push(arr[i] * 2);
  }
  //loop through every number that need to drop and add to the new array.
  for (let i = arr.length - 1; i >= 0; i -= 2) {
    newArr.push(arr[i]);
  }
  //sum the new array and check if the sum module 10 equal 0.
  return newArr.reduce((a, b) => a + b) % 10 === 0;
};

//findInvalidCards store only invalid credit card numbers.
const findInvalidCards = (arr) => {
  //return only the array that is false.
  return arr.filter((newArr) => !validateCred(newArr));
};

const idInvalidCardCompanies = (arr) => {
  let firstDigits = arr.map((fNum) => fNum[0]);
  let companies = [];
  if (
    !firstDigits.includes(3) &&
    !firstDigits.includes(4) &&
    !firstDigits.includes(5) &&
    !firstDigits.includes(6)
  ) {
    console.log("Company not found");
  }
  firstDigits.forEach((each) => {
    if (each === 3 && !companies.includes("Amex")) {
      companies.push("Amex");
    } else if (each === 4 && !companies.includes("Visa")) {
      companies.push("Visa");
    } else if (each === 5 && !companies.includes("Mastercard")) {
      companies.push("Mastercard");
    } else if (each === 6 && !companies.includes("Discover")) {
      companies.push("Discover");
    } 
  });
  return companies;
};

Hi!!

Hi everyone, I got everything to work. It definitely took a lot of Google searching in addition to MDN. I ended up using a slice() method to remove the last number, which wasn’t needed, but works.

I will also probably change the conditionals to a switch statement. Presently, I had to use an ELSE IF statement rather than ELSE to properly log card numbers that didn’t associate with a bank.

Lastly, I used the includes() method to prevent bank names from repeating in an array.

Credit Card Checker

Hello everyone!
It was not easy but, I was able to finish the project.