Challenge Project: Credit Card Checker

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

Hi there! Here is my code for challenge project: Credit Card Checker
I worked on it for about a month time as this project wasn’t easy for the beginners. This solution is beginner’s friendly and easy to understand.
A request to seniors to have a look on my code and guide me to make my code better.

Best Regards…!


// Add your functions below:
// 3-
const validateCred = arr => {   // batch array
arr = arr.slice(); // make a copy of 
// Iterating through for loop from right to left from check digit and skiping every other digit
  for (let i = arr.length - 2; i >= 0; i = i - 2){
    // Every other digit is multiplied by 2
    arr[i] = arr[i] * 2
    // if other digit's answer is greater than 9 then subtract 9 from the value
    if (arr[i] > 9) {
      arr[i] = arr[i] - 9  // new value to arr[i] is stored
    }
  };
    // Declare a total variable to get the sum of all values in an array.
    let total = 0
  // Summing up all elements of array one by one using for loop
    for (let i = 0; i < arr.length; i++){
      total = total + arr[i]
    }
  // Calculating if sum modulo 10 is 0 then the number is valid otherwise invalid
  return total % 10 === 0
};

// In order to log the batch of nested arrays we need to iterate the batch using for loop
 
for (let i = 0; i < batch.length; i++){
  console.log(validateCred(batch[i]))
};

//////////////////////////////////////////////////////

// 4- Creating a function findInvalidCards()
const findInvalidCards = arr => {
    // Declaring a new empty to store invalid cards latter
    let invalidCards = []
  //Role of this function is to check through the nested array to find out for invalid cards using for loop
  for (let i = 0; i < arr.length; i++){
    // let callValidateCred = validateCred(arr[i])

    if (validateCred(arr[i]) === false) {
      invalidCards.push(arr[i])
    }
  }
  return invalidCards
}
let invalidCards = findInvalidCards(batch);
console.log(invalidCards);

/////////////////////////////////////////////////////

// 5- Create a function idInvalidCardCompanies
// Should have 1 parameter and could take nested arrays of invalid card numbers only
// Function should return array of Card companies

const idInvalidCardCompanies = arr => {
  let cardsArray = [] // To store card companies
  
  for (let i = 0; i < arr.length; i++) {
    if (arr[i][0] === 3) {
  // remove duplicate cards in array
      if (!cardsArray.includes('Amex (American Express)')) {
        cardsArray.push('Amex (American Express)');
      }
    } else if (arr[i][0] === 4) {
      if (!cardsArray.includes('Visa')) {
        cardsArray.push('Visa');
      }
    } else if (arr[i][0] === 5) {
      if (!cardsArray.includes('Mastercard')) {
        cardsArray.push('Mastercard');
      }
    } else if (arr[i][0] === 6) {
      if (!cardsArray.includes('Discover')) {
        cardsArray.push('Discover');
      }
    } else {
      console.log('Company not found')
    }
  }  
  return cardsArray

};
console.log(idInvalidCardCompanies(invalidCards))

//////////////////////////////////////////////////

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!Preformatted text

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile: