Credit Card Checker Challenge Project (JavaScript)

In my solution I tried to use as many build-in functions as possible and doing this allowed me to write solutions to some of the functions that were only one line of code.

You may also be interested in how I solved the “validateCred” function. I did a reverse iteration that handles 2 numbers per iteration so the loop only runs half as many times as the array is long.

This is a link to my solution, I hope it helps!

For the creditcard checker first assignment I wrote this code, it works in most cases but some it doesn’t… Can’t seem to find the bug. Can anyone help me?

const validateCred = array => {
  newArray = []
  for(let i = array.length-1; i>=0; i--) {
    if(i %2 === 0){ 
      doubledNum = array[i]*2
      if (doubledNum > 9) {
        doubledNum = doubledNum - 9
      } 
      newArray.push(doubledNum) 
    } else { 
      newArray.push(array[i])
    }
  }
  const sumArray = newArray.reduce((a,b) => {return a+b})
  const isValid = sumArray %10 === 0
  return isValid 
}```

https://github.com/SaqibQaeym/credit-card-checker
Hi, I just finished the project & on line 84 in my code which I copied it from the solution, I couldn’t understand what that code does. can anyone help me with that. Thanks

Looks like it’s looping through all the CC numbers but only looking at the first digit of each. Interesting to see this is the official solution, I went about it a different way.

Hi, I share with you my solution.
https://github.com/slowrocky/credit-card-checker-starter/blob/main/main.js

My solution achieves the same thing in a very similar way

const idInvalidCardCompanies = fullDataSet => {
	const companiesWithInvalidDetails = [];

	const companies = {
		3: 'Amex (American Express)',
		4: 'Visa',
		5: 'Mastercard',
		6: 'Discover'
	};

	for (invalidDetails of fullDataSet) {
		if (companies[invalidDetails[0]]){
			companiesWithInvalidDetails.push(companies[invalidDetails[0]]);
		} else {
			companiesWithInvalidDetails.push('Company not found');
		}
	}

	const uniqueCompanyNames = [...new Set(companiesWithInvalidDetails)];

	return uniqueCompanyNames;
}

Here’s my Gist link for another answer pretty similar to yours: Codecademy export · GitHub

or the same code here:

const validateCred = inputArray => {
	let sum = 0;

	for (let firstIndex = inputArray.length - 2; firstIndex >= 0; firstIndex -= 2) {
		let addToSum = inputArray[firstIndex] * 2;

		sum += addToSum > 9 ? addToSum - 9 : addToSum;
	}

	for (let secondIndex = inputArray.length - 1; secondIndex >= 0; secondIndex -= 2) {
		sum += inputArray[secondIndex];
	}

	return sum % 10 === 0;
};

const findValidCards = input => validCards = input.filter(subArray => validateCred(subArray));

const findInvalidCards = input => invalidCards = input.filter(subArray => !validateCred(subArray));

const idInvalidCardCompanies = fullDataSet => {
	const companiesWithInvalidDetails = [];

	const companies = {
		3: 'Amex (American Express)',
		4: 'Visa',
		5: 'Mastercard',
		6: 'Discover'
	};

	for (invalidDetails of fullDataSet) {
		if (companies[invalidDetails[0]]){
			companiesWithInvalidDetails.push(companies[invalidDetails[0]]);
		} else {
			companiesWithInvalidDetails.push('Company not found');
		}
	}

	return [...new Set(companiesWithInvalidDetails)];
}

console.log(idInvalidCardCompanies(findInvalidCards(batch)));

Sharing a simple solusion

Completed Credit Card Check project this evening. Made use of built-in iterators and knocked it out in just a few lines of code. Added invalid6 and mystery6 to print ‘Company not found’ message. Enjoy!

Hello everyone! These are my solutions in case you were looking for some additional ideas!

Hi,
I have a GitHub account but there is no share code link at the bottom of my Codecademy learning environment.
Is there a way to integrate it?

Here is a link to my gist on github

thanks
Jay

Hi all,

At long last, here is my code!

It took 2 weeks but I was adamant in absolutely refusing to look up other people’s answers or even to check hints. I would only Google references to confirm if an idea I had would work. It was important for me to complete at least 1 project using my own understanding.

// 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 goodCard = 'This credit card number is valid.';

const badCard = 'This credit card number is not valid.';

const validateCred = array => {
  let sumOfOddPlaces = 0;
  let sumOfEvenPlaces = 0;
  for (i = array.length - 1; i >= 0; i -= 2) {
    sumOfOddPlaces += array[i];
  }
  for (j = array.length - 2; j >= 0; j -= 2) {
    if ((array[j]*2) > 9) {
      array[j] = (array[j]*2) - 9
    } else {
      array[j] = array[j]*2
    }
    sumOfEvenPlaces += array[j];
  }
  if ((sumOfOddPlaces + sumOfEvenPlaces) % 10 === 0) {
    return goodCard
  } else {
    return badCard
  }
}

const findInvalidCards = nestArr => {
  let invalidArr = [];
  for (k = 0; k < nestArr.length; k++) {
        if (validateCred(nestArr[k]) === badCard) {
       for (l = nestArr[k].length - 2; l >= 0; l -= 2) {
         if (nestArr[k][l] % 2 === 0) {
           nestArr[k][l] = (nestArr[k][l])/2
         } else {
          nestArr[k][l] = (nestArr[k][l] + 9)/2 
         }
       }  
       invalidArr.push(nestArr[k])
  }
}
return invalidArr
}

const idInvalidCardCompanies = nestArr => {
  let cardCompany = [];
  for (m = 0; m < nestArr.length; m++){
    if (findInvalidCards(batch)[m][0] === 3 && cardCompany.indexOf('American Express') === -1) {
    cardCompany.push('American Express')
  } else if (findInvalidCards(batch)[m][0] === 4 && cardCompany.indexOf('Visa') === -1) {
    cardCompany.push('Visa')
  } else if (findInvalidCards(batch)[m][0] === 5 && cardCompany.indexOf('Mastercard') === -1) {
    cardCompany.push('Mastercard')
  } else if (findInvalidCards(batch)[m][0] === 6 && cardCompany.indexOf('Discover') === -1) {
    cardCompany.push('Discover')
  } else if (findInvalidCards(batch)[m][0] < 3 && findInvalidCards(batch)[m][0] > 6 &&  cardCompany.indexOf('Unknown/Invalid Company') === -1) {
    cardCompany.push('Unknown/Invalid Company')
  }
  }

  return cardCompany
}

// Comment out when ready to log

//console.log(validateCred(invalid1))

//console.log('The following credit card numbers are invalid: ', (findInvalidCards(batch)))

//console.log('The following companies are linked to the faulty credit card numbers: ',(idInvalidCardCompanies(batch)))

Now I can compare code and see where I can refactor.

Hi everybody!

I’m sharing my code with you. I use backcall functions to compute the double of the digits when needed, and simplified the way you find Faulty Companies, investing a bit more on the initializacion of an array but saving code effort.

My solution

Kind regards

Rodolfo

// 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) => { const sumDigits = Array.from(arr); for (let i = sumDigits.length - 2; i >= 0; i -= 2) { let check = sumDigits[i] * 2; if (check > 9) sumDigits[i] = check - 9; else sumDigits[i] = check; } const totalSum = sumDigits.reduce((acc, digit) => acc + digit); return (totalSum % 10) ? false : true; }; const findInvalidCards = (arr) => { const invalidCards = arr.filter((el) => !validateCred(el)); return invalidCards; }; const idInvalidCardCompanies = (arr) => { const companies = []; arr.forEach((el) => { const firstDigit = el[0]; let companyName; switch (firstDigit) { case 3: companyName = 'Amex (American Express)'; break; case 4: companyName = 'Visa'; break; case 5: companyName = 'Mastercard'; break; case 6: companyName = 'Discover'; break; default: companyName = 'Company not found' } const companyCheck = companies.includes(companyName); if (!companyCheck) companies.push(companyName); }); return companies; }; // Test functions: console.log(validateCred(valid5)); // Should print true console.log(validateCred(invalid1)); // Should print false //console.log(findInvalidCards([valid1, valid2, valid3, valid4, valid5]));// Shouldn't print anything //console.log(findInvalidCards([invalid1, invalid2, invalid3, invalid4, invalid5])); // Should print all of the numbers //console.log(findInvalidCards(batch)); // Test what the mystery numbers are console.log(idInvalidCardCompanies([invalid1])); // Should print['visa'] console.log(idInvalidCardCompanies([invalid2])); // Should print ['mastercard'] console.log(idInvalidCardCompanies(batch)); // Find out which companies have mailed out invalid cards

This was a challenging exercise. However, I was able to learn to organize my strategy-making process as well as deal with bugs as I add new functions to the file, especially as I tried to put a function that converts invalid numbers to valid numbers into the mix.

Hi everyone, For this task you can find my solution on github with this link :

i had some trouble with the last function…
Please feel free to leave a comment, thanks!
Enjoy :wink:

CreditCardChecker.js

This project was a real challenge for me, but I am grateful to have done it. I learned so much and feel a lot more confident in my ability to work through problems! Check out my solutions below:

(Credit Card Checker Project I completed on Codecademy. This application takes in credit card numbers and validates them using the Luhn algorithm. Additionally, additional functions will return an array of invalid cards and their associated companies. · GitHub)

Here’s my own solution: (A bit weird for me lol) A feedback would be nice