Credit Card Checker Challenge Project (JavaScript)

First solution for the Credit Card Checker. Would love to see simpler and more concise approaches to this project!

Here is my solution for the credit card numbers checker. any constructive criticism would be greatly appreciated

Thanks you in advance.

Hi guys,

This is my code for the javascript project: Challenge Project: Credit Card Checker

thnx

Here is my Solution for the credit card PROJEKT!!!

// 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 = (Num) => {
  var sum = 0
  for (let i= Num.length - 1; i >= 0; i --){
   if ( i % 2 == 0) 
    { if ((Num[i] * 2) > 9) 
      {var sum = sum + (Num[i] * 2) - 9
      } else
      { var sum = sum + Num[i] * 2}
    } else {  
      var sum = sum + Num[i];
      } 
   }  
let check = sum % 10
if (check === 0){ return true }else {false};
};

var falsecardnum = [];
var correctcardnum = [];
const findInvalidCards = cards => {
for (let i = 0; i < cards.length; i++) { 
    if (validateCred(cards[i]) === true) {
         falsecardnum.push(cards[i])} else {
         correctcardnum.push(cards[i]);  
      };
    } 
};
findInvalidCards(batch);
const idInvalidCardCompanies = falsecardnum => {
  var CreditCardCompanie = []
  for (let i = 0; i < falsecardnum.length; i++){
    if (falsecardnum[i][0] === 3) {
      CreditCardCompanie.unshift('Amex(AmericanExpress)') 
    } else if (falsecardnum[i][0] === 4) {
      CreditCardCompanie.unshift('Visa)') 
    } else if (falsecardnum[i][0] === 5) {
      CreditCardCompanie.unshift('Mastercard)') 
    } else if (falsecardnum[i][0] === 6) {
      CreditCardCompanie.unshift('Discover') 
    } else  {
      CreditCardCompanie.unshift('Company not found') 
    }
  } 
  var mySet = [new Set(CreditCardCompanie)]
  console.log(mySet)
};

idInvalidCardCompanies(falsecardnum)

9 posts were split to a new topic: The correct sum of the doubled array is 29

Hello! I created two files for this project.
The first is where I left my own approach on some problems credit-card-checker/my-version.js at main · makarovangelika/credit-card-checker · GitHub
And the second is where I used recommendations from the hints credit-card-checker/version-from-hint.js at main · makarovangelika/credit-card-checker · GitHub

Hi guys,

This is my code for the Credit Card Checker project

Codecademy Credit Card Checker

Hello everyone
Here is my solution for this project. I used two loops in order to implement the Luhn algorithm! is it a good practice?
please let me know what you think, any comments and suggestions are welcome.

Thank you

Here’s my code! Looking forward to looking through your solutions to see how it compares :slight_smile:

Hey guys, here is my code for this project.
Some feedback would be much appreciated.

Just finished. Posting my solution.

hello everyone, here’s my solution. Any feedback would be very much appreciated

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

let validateCred = (arr) => {
let result = ;
for(let j = arr.length -1; j>= 0; j-=2){
result.push(arr[j])
};
arr.pop();
for(let i = arr.length -1 ; i>= 0 ; i-=2){
let x = arr[i] * 2;
//console.log(x);
if(x > 9){
x = x - 9;
}else if(x <= 9){
x = x;
};
result.push(x);

};
//console.log(result);
const reducer = (a, b) => a + b;
const reduced = result.reduce(reducer);
if(reduced % 10 === 0){
return true;
}else if(reduced % 10 !== 0){
return false;
};
};

const findInvalidCards = (nArr) =>{
let invalidCards = ;
for(let card of nArr){
if(validateCred(card) === false){
invalidCards.push(card);
};

};
return invalidCards;

};

const idInvalidCardCompanies = (invalidCards) =>{
let companies = ;
for(let card of invalidCards){
if(card[0] === 3){
companies.push(‘Amex’);
}else if(card[0] === 4){
companies.push(‘visa’);
}else if(card[0] === 5){
companies.push(‘Mastercard’);
}else if(card[0] === 6){
companies.push(‘Discover’);
}else{console.log(‘Company not found’)};
};
function removeDuplicates(companies){
return companies.filter((value, index) => companies.indexOf(value) === index);
};
return removeDuplicates(companies);

};
//console.log(validateCred(invalid5));
//validateCred(valid2);
console.log(idInvalidCardCompanies(findInvalidCards(batch)));

Hi Everyone! This was a fun challenge. Thanks! Here’s my solution. Feedback welcome. An if anyone’s stuck feel free to use it for inspiration. Codecademy export · GitHub

Finished my Credit Card Checker project!

1 Like
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));