# Credit Card Checker Challenge Project (JavaScript)

I’m burning my brain on this project, can someone take a look a my code please?

1 - in the Luhn algorithm talk about sum the last digit (wich first we remove) with the sum of all the element of the array, so how I make this happen? I can’t find this in some codes I looked.

2 - With my currently code here some of the valids arrays keep showing invalid when I execute the function, but with my noob eyes I’m can’t see the mistake, can someone point out?

``````// 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 => {
let newArray = [];
let reversedArray = arr.slice(0, -1).reverse();

for(let i = 0; i < reversedArray.length; i++){
if(i % 2 !== 0) {
reversedArray[i] *= 2;
if (reversedArray[i] > 9){
reversedArray[i] -= 9;
}
}
newArray.push(reversedArray[i]);

let sum = newArray.reduce((accumulator, number) => {
accumulator + number, 0
});

if (sum % 10 === 0){
return true;
} else {
return false;
}
}
};

console.log(validateCred(valid1));
console.log(validateCred(valid2))

const findInvalidCards = arr => {
let invalidCards = [];
for (let i = 0; i < arr.length; i++) {
if (validateCred(arr[i]) === false) {
invalidCards.push(arr[i])
}
}
return invalidCards;
};

console.log(findInvalidCards(batch));

// output
true
false
[ [ 5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9 ],
[ 3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6 ],
[ 4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 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 ],
[ 5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9 ],
[ 4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3 ],
[ 4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3 ] ]

``````

Hi Julia,

You’re very welcome

Hahaha eventually you’ll want to know both front & back-end. I’m in the full-stack engineering course which teaches both front & back-end.

Oh nice, I’m working on that project now! Maybe we will see each other there .

Well once you get onto discord make sure you add me up I’m always looking to collaborate, for now we’ll keep in touch via this platform!

Happy coding!

Hi Reginag,

It seems on line 30 where you declare `reversedArray` and call `slice` with the parameters of (0, -1) you’re removing the last digit completely from the array that you’re creating.

Keep in mind for the Luhn Algorithm, we want to still add the last number from the original array to our sum function.

You can achieve this in multiple ways:

You can save the last digit into a variable, then add it on in your `sum` variable.

You can loop through the array backwards instead of using .slice(), .reverse() - for your loop initializer you can subtract the array.length by 1 which doesn’t mutate your original array, it just skips the last digit.

I’m sure there are many different solutions to the error you’re running into but those are the ones I can think of off the top of my head.

I would suggest you try to console.log() through your function so you can see exactly what your loops, variables, and return statements look like for the computer.

Happy coding!

I just finished it. You motivated me
No challenges though. I keep those for another day.
I posted under the mysterious organism project if you like to see it. Looking forward to see your solution!

1 Like

That’s awesome glad to hear I motivated you ! I just finished mine.

Tried the challenges, couldn’t figure out the last one.

I’ll take a look at yours shortly and post mine as-well!

Happy Coding!

Hey guys! I finished the credit card checker today and although at first everything seems to work flawlessly, it is unable to verify valid3- i know this happens because it is shorter than the other numbers and since I have not reversed the array but rather just iterated it backwards, there must be some kind of solution to how I duplicate and/or subtract every other number besides mine (on l. 30). My head is knotting itself in a twist though, can anyone give me some advice?

Hi gooddevenin,

I finally finish the project and your help was extremely important, I choose store the last digit in a variable and worked fine, but also haved a little mistake with my logic on the loop and well, took some time to figure out .lol

Here is my code and I accept any feedback.

happy coding!

``````// 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 => {
let newArray = [];
let reversedArray = arr.slice(0, -1).reverse();
let lastDigit = arr[arr.length - 1];
//console.log(reversedArray);
// console.log(lastDigit);

for(let i = 0; i < reversedArray.length; i++){
if(i % 2 == 0) {
reversedArray[i] *= 2;
if (reversedArray[i] > 9){
reversedArray[i] -= 9;
}
}
newArray.push(reversedArray[i])
//console.log(reversedArray[i]);
}
//console.log(newArray);
let sum = newArray.reduce((accumulator, number) => accumulator + number, 0) + lastDigit;
//console.log(sum);
return sum % 10 === 0 ? true : false;
};

//console.log(validateCred(valid1));
//console.log(validateCred(valid2));
//console.log(validateCred(valid3));
//console.log(validateCred(valid4));
//console.log(validateCred(valid5));
//console.log(validateCred(invalid2));
//console.log(validateCred(mystery1));

const findInvalidCards = arr => {
let invalidCards = [];
for (let i = 0; i < arr.length; i++) {
if (validateCred(arr[i]) === false) {
invalidCards.push(arr[i])
}
}
return invalidCards;
};

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

const idInvalidCardCompanies = arr => {
let invalidArrays = findInvalidCards(arr);
let companies = [];
console.log(invalidArrays);

for (let i = 0; i < invalidArrays.length; i++){
switch (invalidArrays[i][0]){
case 3:
companies.push('Amex');
break;
case 4:
companies.push('Visa');
break;
case 5:
companies.push('Mastercard');
break;
case 6:
companies.push('Discover');
break;
default:
companies.push('Company not found');
break;
}
}

let invalidCardCompanies = [ ...new Set(companies) ];
return invalidCardCompanies;
};

console.log(idInvalidCardCompanies(batch));

//output

[ [ 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 ],
[ 6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3 ],
[ 4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3 ] ]
[ 'Visa', 'Mastercard', 'Amex', 'Discover' ]
``````
1 Like

Woohoo, this was fun! I only had to reset all of my JS modules and re-do them all a second time before I had any thoughts about being able to complete what I thought was a very daunting project. Second time through and I didn’t even need to consult the help sections for ideas! My solution can be found here on github:

Hi Reginag,

That’s amazing! I’m glad I was able to help you out.

Happy Coding!

Phew, finished!!

Here’s what I came up with:

All the tests are coming back as they should!

Hey everybody sharing a project for the first time, completed this without peeping at the solution code, so an honest opinion on how to tackled this would be greatly appreciated, thanks in advance!

Here is my code for the challenge!

Hi, guys!

Here’s my solution code, so you guys can compare

Quick note: i tried to use ternary on the switch statement in the last function, but it kept having issues with the ‘continue’ keyword, so i had to implement long if…else statements, making the code a little too verbose. You’re free to improve it if you feel like it!

Kudos for completing your project as well, and if you’re stuck or interested, try taking a look on mine!

Here’s a repl.it with it: https://replit.com/@ProspektsMarch2/Validador-de-Cartao-Demoniaco

Here is my solution:
any feedback is appreciated

My initial attempt at the validateCred() function involved checking if the arrays were even or odd in length and using that info to modify every other digit. It worked when tested by itself but was somehow permanently modifying the arrays so that the following two functions would not work properly.

The idea to loop through the array backwards was what I went to as this was my original idea I thought of but I struggled for hours to figure out a method to modify every other digit correctly. I often find solutions to my issues on stack overflow or elsewhere but sometimes choose not to implement them as I can’t always understand the logic of why they work.

And when something like this is presented as, “something I found and would like to understand” we are only too happy to provide insight. Just so long as it is not something posted as a solution to an exercise.

That over with, definitely bring your questions to the forums. Others can learn from it, too. What we want to prevent is the ‘loss of our youth’ because we were too busy trying to be adults. There is so much to behold in the very rudimentary of code that we should never want to stray too far from it. It’s like riding a bike. That’s the perfect analog.

I think it works so I am chuffed

Hi,

Thanks for sharing your standard solution. I solved the last question by creating an object within a function. In general, is it a good practice generating an object within function? My code is below for your reference:

``````function idInvalidCardCompanies(nested) {
results = [];
const company_obj = {
3: 'Amex (American Express)',
4: 'Visa',
5: 'Mastercard',
6: 'Discover'
};
for (const arr of nested) {
//console.log(company_obj[arr[0]]);
//console.log(!(arr[0] in company_obj));
if (!(arr[0] in company_obj)) {
console.log('Company not found');
} else {
if (!(results.includes(company_obj[arr[0]]))) {
results.push(company_obj[arr[0]]);
}
}
}
return results;
}
``````

Hi, all. Just setup a new Github account and wanted to share my code with some debugging code still left inside. Long day but so just want to submit this but can’t wait to take a look at the official solution and others code.