Hello everyone!
I just finalized my Credit Card Checker project. I’ve used everything in my mental toolbox that I’ve put together with Codecademy thus far. Please let me know if there is anything I can do to improve this code. Thanks!
Credit Card Checker repository on GitHub
Hi here is my solution:
hey @markamarzan nice one
i saw your solution.
Its kinda same with me.
Do check my solution in github at here: GitHub - amanc1248/creditCardValidater
However if you want to go along with the full stack journey do let me know.
Find me at linkedin: https://www.linkedin.com/in/aman-chaudhary-4a8725173/
I really enjoyed this project (after banging my head on the desk a few times after I got stuck), because I got introduced to Set.
Check it out at step 5 of the code:
const validateCred = function (arr){
let a = [ ];
let b = [ ];
let newArr = [ ];
//for loop to seperate array in two arrays, one (a) for *2 and one(b) for *1
for(let i = 0; i < arr.length; i++){
if(i % 2 === 0){
a.push(arr[i]);
} else {
b.push(arr[i]);
};
};
//verified a and b have the correct data in the array
for(let k = 0; k < a.length; k++){
a[k] = a[k] * 2;
};
//verified a now contains each element * 2
for(let j in a){
if(a[j] >= 10){
a[j] = a[j] - 9;
} else {
a[j] = a[j];
};
};
//verified array a has every element > 9 subtracted by 9
newArr = newArr.concat(a, b);
// verified newArr holds the value of arrays a and b
arrSum = newArr.reduce((x, y) => x + y);
// verified arrSum correctly adds all elements in newArr
if(arrSum % 10 === 0){
return true;
} else {
return false;
};
//verified validateCred properly returns true or false
};
//step 4
const findInvalidCards = function(arr){
let c = [ ];
for (let i = 0; i < arr.length; i++){
if (validateCred(arr[i]) === false){
c.push(arr[i])
};
};
return c;
};
//verified findInvalidCards returns invalid arrays
//step 5
const idInvalidCardCompanies = function(arr){
let d = [ ];
for (let i = 0; i < arr.length; i++){
if (arr[i][0] === 3){
d.push('Amex');
} else if (arr[i][0] === 4){
d.push('Visa');
} else if (arr[i][0] === 5){
d.push('Mastercard');
} else if (arr[i][0] === 6){
d.push('Discover');
} else {
d.push('Company not found')
};
};
e = new Set(d);
//uses Set to eliminate duplicates
f = Array.from(e);
//converts Set e to an array
return f;
};
// verified idInvalidCardCompanies() returns names using below variable 'invalid'
invalid = [[ 3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6 ],[ 4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5 ],[ 5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3 ],[ 3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4 ],[ 6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5 ],[ 5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4 ],[ 3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4 ],[ 4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3 ]];
Hey!
Well done on completing the project! When sharing code on the forums be sure to format it according to this guide: How do I format code in my posts?, it just makes it a lot easier to see/read the code.
Happy coding!
Hi! I realized how useful the methods are.
const validateCred = arr => {
let sum = 0;
for(let i = arr.length - 1; i >= 0; i--){
if(!(i%2===0)){
sum += arr[i];
}else{
let doubled = arr[i]*2;
if(doubled > 9){
doubled -= 9;
sum += doubled;
}else{
sum += doubled;
}
}
}
return sum % 10 === 0;
}
const findInvalidCards = arr => {
let arr2 = arr.filter(element => !(validateCred(element)))
return arr2;
}
const idInvalidCardCompanies = arr => {
let arr2 = [];
for(let i = 0; i < arr.length; i++){
switch(arr[i][0]){
case 3:
arr2.push('Amex');
break;
case 4:
arr2.push('Visa');
break;
case 5:
arr2.push('Mastercard');
break;
case 6:
arr2.push('Discover');
break;
default:
console.log('Company not found');
}
}
let arr3 = arr2.filter((item,index)=>{
return arr2.indexOf(item) === index;
})
return arr3;
}
Hi everyone!
In my code, I tried to use Iterator functions (.filter(), some(), map(), reduce()):
Hey guys here’s my solution to this code challenge
I know it’s not the most efficient way if doing it but I managed to figure it out on my own which I’m happy with.
My code is given below. You can also check out my GitHub: creditCardChecker/creditCardChecker.js at main · ahman502/creditCardChecker · GitHub
Please run the program above to see the output. My output is very long and extensive so I couldn’t copy-paste it. I did this problem the long way but I understood exactly what I was doing so I am happy with this solution. However, I would also love your feedback or any suggestions to improve this code. Thank you!
Here is my Credit Card Checker Challenge Project.
GitHub - GitHub - rajevesubnaik/Credit-Card-Checker-Project: check whether a credit card is valid or invalid
// 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:
// determin if card number is valid or invalid
const validateCred = credArray => {
// make copy of origninal array so we don't modify
const copyArray = [...credArray];
// reverse the array
const reverseArray = copyArray.reverse();
// remove to check digit
const checkDigit = reverseArray.shift();
// loop through every other digit in array and double. If double is > 9 then subtract 9
const everyOther = [];
for (let i = 0; i < reverseArray.length; i++) {
if (i % 2 == 0 ) {
const double = reverseArray[i] * 2;
if (double > 9) {
const notDouble = double - 9;
everyOther.push(notDouble);
} else {
everyOther.push(double);
}
} else {
everyOther.push(reverseArray[i]);
}
}
// add back the check digit
everyOther.unshift(checkDigit);
// find the sum of the array
const sum = everyOther.reduce((prevVal, currentVal) => prevVal + currentVal);
// determine if the card is valid or invalid
const isValid = sum / 10 % 2 === 0 ? true : false;
return isValid;
}
// return invalid card numbers
const findInvalidCards = credCard => {
const invalidCard = [];
// loop through an array of card numbers and determine ones that are invalid
for (let i = 0; i < credCard.length; i++) {
if (!validateCred(credCard[i])) {
invalidCard.push(credCard[i]);
}
}
return invalidCard;
}
// determine which card company issued invalid cards
const idInvalidCardCompanies = invalidCard => {
const cardCompanies = [];
for (let i = 0; i < invalidCard.length; i++) {
switch (invalidCard[i][0]) {
case 3:
cardCompanies.push('amex');
break;
case 4:
cardCompanies.push('visa');
break;
case 5:
cardCompanies.push('mastercard');
break;
case 6:
cardCompanies.push('discover');
break;
default:
console.log('Company not found');
break;
}
}
// remove duplicate card companies after iterating array
const removeDuplicate = [... new Set(cardCompanies)];
return removeDuplicate;
}
// convert a number string to an array
const numStringToArray = string => {
return string.split('').map(Number);
}
console.log(findInvalidCards(batch));
console.log(idInvalidCardCompanies([invalid1, invalid2, invalid3, invalid4, invalid5]));
console.log(numStringToArray('4744677908016808'));
Here is my Credit Card Checker Challenge Project - check whether a credit card is valid or invalid
Hello to everyone!
Here is my solution:
I think I’ve managed to complete this task, but I’d love to know what glaring improvements I could have made!
Hi everyone, I have completed this challenge. Would really appreciate any feedback, please. Thank you.
Hello, this is my solution.
PS, if you are looking for motivation: i started the full-stack path a long time ago i always broke down whenever i couldn’t get a solution (mostly with JS II part) and dropped the path 2 times. Then i decided i should start over my journey of coding by taking de CS path from CC. Now, i finally get to publish my own solution for these type of challenges and i’m super happy You’ll get there!
I also noticed this. Maddening lol – Codecademy needs to address this