Credit Card Checker Challenge Project (JavaScript)

Hello! I was hoping someone could help me with a code review. My code seems to work perfectly for this exercise and passes all tests except for one.

For some reason, it detects ‘valid3’ as invalid. Would anyone be able to tell me why?

I’ve included a link to my code:

https://gist.github.com/8de2110abce4e1b4ecca440efd1b4a3e

That valid3 is a different length than the other credit card numbers.
Your code for validateCred works fine when the length of the card array is an even number,
but not for odd lengths.

This is because you manipulated the stuff at even-numbered indices (multiplying by 2, subtracting 9 if needed), but this only matches the algorithm when the length of the card numbers array is even.
(You would manipulate the stuff at the odd indices if the length of the card was an odd number.)

1 Like

Hello!

This is my solution to the project. Would appreciate some feedback as this is one of my first off-platform projects. Thanks so much!

Thanks a lot that’s very helpful!

Hey everyone!

I need to share a personal and empowering story before I share the link to my completed code.

I enrolled in this course over a year ago, and back then, I was flying through it and loving every minute.

While the challenges and course were challenging, nothing made me “stuck,” and I kept moving and going.

I loved the collaborative challenges and was excited about my javascript coding.

Then…the credit card checker challenge came.

I’m not going to lie; this is one tough nut to crack for a newbie.

No “get unstuck” video and no tutorials online.
Sure, there were solutions online, but I didn’t want to copy-paste someone’s answer and not understand it.

I remember spending hours looking at others’ solutions; it felt like a black sea was swimming in front of my eyes.

I didn’t intend to, but slowly, I kept plugging away at it less and less until I eventually stopped plugging away.

A whole year went by.

Then, the other night, I decided to load it back up on a whim.
“■■■■, I pay $$$ a year for this, might as well use it a bit.”

I’m happy to report that after a year’s hiatus, I successfully completed this challenge and am back in the swing of it.

I had to use help, spend several hours analyzing other’s solutions, and coming up with novel ways to do it on my own, but I did it. No copy-pasting; I finally understand every line in my code.

Here it is on GitHub; comments are appreciated!

2 Likes

My gist for credit card checker

I did with my own way too!

// 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];

const validateCard = arr =>  {
    let temp = []
        if (arr.length % 2 === 0){
            for (let i = 0; i < arr.length; i++) {
                if ((i + 1) % 2 === 0) {
                    temp.push(arr[i])
                } else {
                    temp.push(arr[i] * 2)
                }
            }
        }
        else if (arr.length % 2 === 1){
            for (let i = 0; i < arr.length; i++) {
                if ((i + 1) % 2 === 0) {
                    temp.push(arr[i] * 2)
                } else {
                    temp.push(arr[i])
                }
            }
    }
    temp = temp.map(x => x < 10 ? x : x - 9)
    return temp
}

const result = arr => {
    let temp = validateCard(arr)
    let temp2 = temp.reduce((a, b) => a + b)
    if (temp2 % 10 === 0) {
        console.log(`This is valid card number. card number: ${temp.join("")}`)
    }   else {
        console.log(`This is NOT valid card. Please check the number.`)
    }
}

I expect to see an instruction for dropping the last digit, but i don’t see it in the solution? Can someone delight me? :slight_smile:

Here is my solution!

// 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: function validateCred (inputArray) { let sumOfCreditCardNumbers = 0; let j = 0; let validation; for (let i = inputArray.length - 1 ; i > -1; i--){ j++; if( j%2 > 0) { sumOfCreditCardNumbers = inputArray[i] + sumOfCreditCardNumbers; } else { if(inputArray[i] * 2 > 9){ sumOfCreditCardNumbers += (inputArray[i] * 2) - 9; } else { sumOfCreditCardNumbers += (inputArray[i] * 2); } } //console.log(j,inputArray[i],sumOfCreditCardNumbers); } if(sumOfCreditCardNumbers % 10 === 0){ validation = true; } else { validation = false; } return validation; } function findInvalidCards (nestedArray) { let newArray = []; for(let i=0; i <nestedArray.length; i++ ){ validateCred(nestedArray[i]); if(validateCred(nestedArray[i]) === false){ newArray.push(nestedArray[i]) } } //console.log(newArray); return newArray; } function idInvalidCardCompanies (invalidNumberArray) { let invalidCardCompany = []; let h=-1; for(let i=0; i < invalidNumberArray.length; i++){ h++; for(let j=0; j < 1; j++){ if(invalidNumberArray[h][j] === 3 && invalidCardCompany.indexOf('Amex (American Express)') < 0 ){ invalidCardCompany.push('Amex (American Express)'); } else if (invalidNumberArray[h][j] === 4 && invalidCardCompany.indexOf('Visa') < 0){ invalidCardCompany.push('Visa'); } else if (invalidNumberArray[h][j] === 5 && invalidCardCompany.indexOf('Mastercard') < 0){ invalidCardCompany.push('Mastercard'); } else if (invalidNumberArray[h][j] === 6 && invalidCardCompany.indexOf('Discover') < 0){ invalidCardCompany.push('Discover'); } else if (invalidNumberArray[h][j] < 3 || invalidNumberArray[h][j] > 6){ invalidCardCompany.push('Company Not Found') } } } console.log(invalidCardCompany) return invalidCardCompany; } idInvalidCardCompanies(findInvalidCards(batch))

I feel like my solution may be a little bit jenky and long written, however, I do appreciate feedback on how I could have wriitten my code better.

Hi,
I try to do this. But I think not work. Check please

Hi guys, here is my solution for anyone that needs a reference:

// 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 sum = 0; for (let i = arr.length - 1; i >= 0; i--) { //console.log(); if (arr.length % 2 === 0 && i % 2 === 0) { arr[i] *= 2; if (arr[i] > 9) { arr[i] -= 9; //console.log(arr[i]); sum += arr[i]; } else { //console.log(arr[i]); sum += arr[i]; } } else if (arr.length % 2 != 0 && i % 2 != 0) { arr[i] *= 2; if (arr[i] > 9) { arr[i] -= 9; //console.log(arr[i]); sum += arr[i]; } else { //console.log(arr[i]); sum += arr[i]; } } else { //console.log(arr[i]); sum += arr[i]; } } //console.log(sum); return sum % 10 === 0; }; //console.log(invalid3); //console.log(validateCred(valid3)); const findInvalidCards = (arr) => arr.filter((val) => !validateCred(val)); //console.log(findInvalidCards(batch)); const idInvalidCardCompanies = (arr) => { let invalidCards = findInvalidCards(arr); let comps = []; for (let i in invalidCards) { if ( invalidCards[i][0] === 3 && !comps.includes("Amex (American Express)") ) { comps.push("Amex (American Express)"); } else if (invalidCards[i][0] === 4 && !comps.includes("Visa")) { comps.push("Visa"); } else if (invalidCards[i][0] === 5 && !comps.includes("Mastercard")) { comps.push("Mastercard"); } else if (invalidCards[i][0] === 6 && !comps.includes("Discover")) { comps.push("Discover"); } else { console.log("Company not found"); } } return comps; }; //console.log(idInvalidCardCompanies(batch));

Hey, it works, right? So that’s good, but in your idInvalidCardCompanies function, you could have avoided using an inner for loop to check the first digit of the cards. Instead, you have just checked by using invalidNumberArray[h][0] === (number). And to simplify the second part of those if statements in there, you could have used !invalidCardCompany.includes(‘Visa’).

Thanks so much for the feedbaack! Before learning Javascript I learned C, and C++, so I’m sticking to what I know for now. However I’m still learning to use every function that has been taught so far, but I always seem to forget :smile:. Any tips to keep functions at hand?

Feeling good with my end code, looking forward to more of these, Github link

No questions but thought I’d share my solution. I appreciate you all. I could refactor plenty. First thing that sticks out is making a validation variable for the validateCred function to make it easier to understand what is true and false.

// All valid credit card numbers const valid1 = [9, 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]; const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5]; function validateCred(arg) { let i = 0; let evenArray = [] let moduloArray = []; arg.reverse().forEach(num => { if(i % 2 != 0) { num = num * 2; if(num > 9) { num = num - 9; } moduloArray.push(num) }else{ evenArray.push(num); } i++; }); moduloArray = moduloArray.concat(evenArray); sum = moduloArray.reduce((a,b) => a + b, 0); arg.reverse(); if(sum % 10 === 0) { return true }else{ return false } }; function findInvalidCards(arg){ const invalidCards = [] let i = 0; arg.forEach(card => { if(validateCred(card[i]) === false) { invalidCards.push(card) } }) return invalidCards } function idInvalidCardCompanies(arg){ const companies = []; arg.forEach(card => { if(card[0] === 3){ companies.push("Amex (American Express)") } if(card[0] === 4){ companies.push("Visa") } if(card[0] === 5){ companies.push("Mastercard") } if(card[0] === 6){ companies.push("Discover") } if(card[0] > 6 || card[0]< 3) { console.log("Company Not Found") } }) return [...new Set(companies)]; } console.log(idInvalidCardCompanies(batch));
1 Like

I got there in the end. :raised_hands: Many ways to kill a cat, here is mine:

Looking clean! I’ve been forgetting to use some of the shortcuts like the ‘for in’ loop and switch. Gotta look over my review folders before I start the next project lol

1 Like

My brain is spinning, but I got it to work! Feeling pretty clever with some of my solutions, but probably doing some things that are not ‘best practice’. If anyone wants to pick apart my code feel free!

This one took some hours. Feels veeeeery good to be getting comfortable with JavaScript. Pretty much like taming a beast… Anyways, my solution is down here :point_down:

HERE’S THE REPO

Thanks everyone, have a nice day or night :v:

Haha, thanks bud. Yeah, the ‘for in’ really helps reduce silly increment errors and the switch statement works great in instances with many potential cases/scenarios instead of just like 1, 2 or 3.