Credit Card Checker Challenge Project (JavaScript)

I think that’s larger than the largest possible integer in JavaScript.
I guess you could use an array or string or BigInt instead.

1 Like

This was a fun project. Spent some time refactoring the original code. No doubt it could still be improved. Any positive feedback is quite welcome!!

GitHub Link to my credit card challenge

Hi, I am not understanding how you used :

        if (companyArray.indexOf("Visa") === -1) {

While, this how I did it :

    } if(invalidCompanyNames.indexOf('Visa') !== -1){

Though I did not use a switch statement, just a list of if statements, so maybe thats why…
Switch statements are not my strong suit LOL.

This project is really challenging, I got the first section help online, the second section myself but the third section was so confusing but after reviewing some other code, I finally got one.
check out my project here: credit-card-checker/credit card checker at main · aladesaye/credit-card-checker · GitHub

Hi Kailiea,
Thanks for the msg and taking the time to look at my code. As you know, “The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.”

So, the ‘if’ statement looks in the array I declared to hold the company names and is checking if ‘Visa’ is already in the array. If it is not yet in the companyArray, then the .push() method adds Visa to the array. The next time through, if another card number has 3 as the first digit, meaning it is a Visa card number, the condition in the ‘if’ statement will return false and not add ‘Visa’ to the array again. It’s how I prevent duplicates from being written to the array.

I hope this helps! There are lots of ways to look at how to avoid duplicates, so as long as your code does that, it is correct! :slight_smile:

1 Like

This project really kicked my butt!

GitHub : GitHub - Karbeau/Codecademy-Credit-Card-Checker

Thanks for the reply!
I looked over your code again and understand it now,
I did mine in 2 steps so its a bit different.

Hey everyone just got done with this project and wanted to post it here for any comments anyone may have. Thanks in advance for checking it out!

Hello! I’ve completed the task. My code is still sh*tty but I’m happy :smiley:

1 Like

This stumped me as well, and the answers here helped a lot to clear it up but ultimately this is how I was able to make sense of it in my head! Hope this helps someone, it will help me a lot writing it out!

numArr.length -1 -i : essentially iterates through the array in reverse. Which allows us to exclude the last digit(check number) and begin alternating every other number in the array from that direction.

numArr.length : provides the length of the array.
-1 : subtracts one, in this case to account for the check number we want to exclude when we begin alternating through the indices.
-i : subtracts the current index as defined in the for loop, which would be zero as per - for( let i = 0; i <= numArr.length; i++)

So for an array with a length of 16, we subtract one giving us 15 which again excludes the last digit from being included when we start to alternate, then we subtract i as defined in the for loop, which will initially be 0, leaving us with 15. As the loop iterates, i will increase in value, incrementally subtracting from 15.

% 2 === 1 : Now that we have defined the range we want to alternate in, we can use % 2 to alternate through our array by checking whether the current index is an even or odd number. I found that using a console.log((numArr.length - 1 - i) % 2 === 1) directly under the if statement that checks for it, helped enormously visualizing what is going on.

I don’t think this is ■■■■■■ code! You’re way of filtering the invalid numbers into the providing companies was much shorter than my gangly code. I tried using forEach and kept accidentally modifying the arrays. I’m going to study your code to figure out where I was going wrong.

This was a toughie! Here’s my solution:

Hi Everyone!

This was a great exercise, here is my version with some tweaks added.

I hope you have a great week and happy learning.



Here goes my version of the solution: