Credit Card Checker Challenge Project (JavaScript)

Here is my solution. If anyone has questions drop them below

valid 3 is actually invalid because it has 15 digits instead of 16, please check

I made this. When I compared it to the solution code, I found my code had done basically the same

i was intrigued by that as well, but apparently a valid card number can technically have anywhere between 8 and 19 digits. In really, it seems that most of them are 15-19 digits long though. [Source]

1 Like

Wow, it’s funny to see that (as far as I saw) no one has the same solution. This is my solution: GitHub - FDeveloperTest/CreditCardChecker: Project given by Codecademy.

Here’s my fix :slight_smile:

GitHub - Credit Card Checker (main.js)

Hi everyone! Here is my solution to this challenge problem:

GitHub Repo - Credit Card Checker

Any feedback will be greatly appreciated! :slight_smile: Happy coding!

Heyy I hope I am not the only one struggling with coding :slight_smile:
here is my code for the credit card project. If you have any comments or suggestions feel free:
https://github.com/mattibaus/projects/blob/main/JavaScript/credit-card-checker-starter/main.js
best,
matti

here is my code if there is anybody interested in crosschecking, I noticed that we all used different approaches , you may find it interesting

I think you could have done this instead of consolelogging 15 times:
const resultsValid = [valid1, valid2, valid3, valid4, valid5].map(validatedCred);

const resultsInvalid = [invalid1, invalid2, invalid3, invalid4, invalid5].map(validatedCred);

const resultsMystery = [mystery1, mystery2, mystery3, mystery4, mystery5].map(validatedCred);

// Add your functions below:
function validateCred(card) {
  function sumDoubledDigits(card) {
    let sum = 0;
    for (let i = card.length - 2; i >= 0; i -= 2) {
      let doubled = card[i] * 2;
      if (doubled > 9) doubled -= 9;
      sum += doubled;
    }
    return sum;
  }
  
  function sumUndoubledDigits(card) {
    let sum = 0;
    for (let i = card.length -1; i>= 0; i -= 2) {
      sum += card[i]
    }
    return sum;
  }
    
  const total = sumDoubledDigits(card) + sumUndoubledDigits(card);
  return total % 10 === 0;
};

function findInvalidCards (cards) {
  return cards.filter(card => !validateCred(card))
};


function idInvalidCardCompanies(cards) {
  const companies = {
    3: "Amex",
    4: "Visa",
    5: "Mastercard",
    6: "Discover"
  };

  const result = new Set();

  cards.forEach(card => {
    const company = companies[card[0]] || "Company not found";
    result.add(company);
  });

  return [...result];
}

Hello fellow Codecademy students! :waving_hand:

Here’s my solution to the Credit Card Checker project from the Full Stack Engineer Career Path.


:file_folder: GitHub Repo:
:backhand_index_pointing_right: GitHub - bastianrecr/credit-card-checker


:brain: What I implemented:

  • :white_check_mark: validateCred() to check if a card number is valid (Luhn algorithm).
  • :white_check_mark: findInvalidCards() to filter out all invalid cards from a batch.
  • :white_check_mark: idInvalidCardCompanies() to identify which companies issued the invalid cards.
  • :white_check_mark: cardNumberToArray() to convert a string input into an array of digits.
  • :white_check_mark: fixInvalidCardNumber() to adjust a card number and make it valid.

:laptop: script.js Preview:

js

CopyEdit

// Validates a credit card number using the Luhn algorithm
const validateCred = (arr) => {
  const reversedArr = [...arr].reverse();
  const sum = reversedArr.reduce((acc, num, i) => {
    const value = i % 2 === 1 ? (num * 2 > 9 ? num * 2 - 9 : num * 2) : num;
    return acc + value;
  }, 0);
  return sum % 10 === 0;
};

// Filters out and returns all invalid card numbers from a batch
const findInvalidCards = (arr) => {
  return arr.filter((element) => !validateCred(element));
};

// Identifies the companies that issued invalid card numbers
const idInvalidCardCompanies = (arr) => {
  const result = [];
  const add = (company) => {
    if (!result.includes(company)) result.push(company);
  };
  for (const element of arr) {
    switch (element[0]) {
      case 3: add("Amex"); break;
      case 4: add("Visa"); break;
      case 5: add("Mastercard"); break;
      case 6: add("Discover"); break;
      default: console.log("Company not found"); break;
    }
  }
  return result;
};

// Converts a card number string into an array of digits
const cardNumberToArray = (cardNumber) => {
  return cardNumber.split("").map(Number);
};

// Takes an invalid card number and adjusts it to make it valid
const fixInvalidCardNumber = (arr) => {
  if (validateCred(arr)) return arr;
  const reversed = [...arr].reverse();
  const sum = reversed.reduce((acc, num, i) => {
    const val = i % 2 === 1 ? (num * 2 > 9 ? num * 2 - 9 : num * 2) : num;
    return acc + val;
  }, 0);
  const remainder = sum % 10;
  const difference = 10 - remainder;
  reversed[0] = (reversed[0] + difference) % 10;
  return reversed.reverse(); // <- typo fixed here
};

:magnifying_glass_tilted_left: What I’m looking for:

I’d love your feedback on:

  • Code efficiency and clarity
  • Any edge cases I might have missed
  • Ideas for how I could improve or extend the project

Thanks in advance! :raising_hands:
Happy coding, everyone!