Hello, after putting in a day and a half of effort, I completed the challenge with some assistance. I’m balancing work and study commitments, and I’m doing my best to dedicate time to learning.
Git hub Repositiry
Hello, after putting in a day and a half of effort, I completed the challenge with some assistance. I’m balancing work and study commitments, and I’m doing my best to dedicate time to learning.
Git hub Repositiry
View my code and be the judge:
Found this one quite tough, never would of thought of completing the validateCred function in the way provided by the solution - feels like I still have a lot to learn. Wish there was a video to go along with this to try and understand the steps it took to reach the given solution too.
Gave up on trying to find a way to convert invalid numbers to valid ones, my brain just couldn’t work it out. Has anyone got a solution or tried to do it?
Here’s my solution to the challenge so far: https://github.com/MagonBorn/CreditCardChecker/blob/main/app.js
Matt
You have a minor typo at the decremental operator part within the first loop (should be i--
)
Variables defined within an if or else block are scoped only to that block and cannot be accessed outside of it. In your case, uniqueFirstNumbers
is defined either within the if block or the else block, and you’re trying to access it outside of those blocks, which results in a ReferenceError. To fix this, you need to declare uniqueFirstNumbers
outside of the if-else block, so it’s accessible in the broader scope of the function.
Hi all here is my version, I see now I overcomplicated a few lines but I guess that is the part of the process
I have a hard time learning javascript. I put the effort and understand how it articulates and the logic behind it but the more I do those exercises, the more I ask myself if this way of learning is obsolete.
I completed the exercise in 5 minutes with chat GPT. In real life, if I need to solve a problem, I would also turn to a GPT. So what’s the point ?
I’m 45. I studied the old-way, pre-internet, with books. Relying on AI to solve problems seemed wrong at first but then I remember the time when I had to remember telephone numbers and how I stopped learning them the moment I had a smartphone. Well, it feels like I don’t need to rely on MY intelligence to solve problems anymore.
Maybe there’s a way for codecademy to recycle it’s current material and focus on how to articulate questions more than finding answers.
Can’t wait to finish this chapter and move on to building website again.
Ok, so I think my code works well, but after looking at some other solutions, I think I did it way longer than I should’ve haha. Here it is:
const validCards = [];
const invalidCards = [];
const validateCred = arr => {
let rawArr = arr.join('');
const lastDigit = arr[arr.length - 1];
const reversedNums = [];
const oddDigits = [];
const restOfDigits = [];
const processedDigits = [];
for (let i = arr.length - 2; i >= 0; i--) { // Rellena el array 'reversedNums' con los numeros invertidos del array (menos el ultimo digito)
reversedNums.push(arr[i]);
}
for (let i = 0; i < reversedNums.length; i = i + 2) { // Rellena el array 'oddDigits' con "numero por medio"
oddDigits.push(reversedNums[i]);
};
for (let i = 1; i < reversedNums.length; i = i + 2) { // Rellene al array 'restOfNumbers' con el resto de los numeros.
restOfDigits.push(reversedNums[i]);
}
for (let i = 0; i < oddDigits.length; i++) { // Rellena el array 'processedDigits' con el resultado de la multiplicacion de los digitos *2 y -9 en el caso de los que sean mayores a 9
let multiply = (oddDigits[i] * 2);
if (multiply > 9){
multiply = multiply - 9;
}
processedDigits.push(multiply);
};
const finalNumber = restOfDigits.concat(processedDigits);
const result = finalNumber.reduce((a,b) => a+b);
const addDropped = result + lastDigit;
const isValid = numToCheck => {
if (addDropped % 10 === 0) {
validCards.push(`${rawArr}`);
return 'Valid Credit Card';
} else {
invalidCards.push(`${rawArr}`)
return 'Invalid Credit Card';
};
}
return isValid(addDropped)
};
const mapCC = batch.map(validateCred);
const idInvalidCardCompanies = arr => {
const invalidCompanies = [];
arr.forEach(ccnumber => {
if (ccnumber[0] === '3' && invalidCompanies.indexOf('Amex(American Express)') === -1) {
invalidCompanies.push('Amex(American Express)')
} else if (ccnumber[0] === '4' && invalidCompanies.indexOf('Visa') === -1) {
invalidCompanies.push('Visa')
} else if (ccnumber[0] === '5' && invalidCompanies.indexOf('Mastercard') === -1) {
invalidCompanies.push('Mastercard')
} else if (ccnumber[0] === '6' && invalidCompanies.indexOf('Discover') === -1) {
invalidCompanies.push('Discover')
} else {
return 'Company not Found'
}
} );
return invalidCompanies;
}
console.log(idInvalidCardCompanies(invalidCards));
console.log(`Valid Card Numbers: ${validCards}`);
console.log(`Invalid Card Numbers : ${invalidCards}`);
Jims Credit Card Checker solution
Hi, Just completed this,
I found it difficult but rewarding to see it working at the end.
Something I also try to do when I get these to work is to attempt to condense the lines of code, I initially had some helper functions in but was able to shorten the code and eliminate the need for these. Yes it took longer to complete but I was happier with the final result as I felt it was less muddled and easier to follow.
I did wonder if these more difficult projects where meant to follow on from the pairing section, giving 2 people something with more teeth to work on. I haven’t managed to pair with anyone yet on a project so if you are in the same boat then maybe we can help each other out, so feel free to contact me.
best of luck with the rest of the course
Jim
I was pretty excited to finally complete this - this was definitely the toughest project I’ve come across so far as a new coder.
After viewing the provided solution, I feel a little deflated because of how different it is and how much more efficient the code is. For example it didn’t occur to me that returning the result of the first part mod 10 could be done in one line by simply returning the result. I did an if/else to return true/false
if (sum % 10 == 0){
return true;
} else {
return false;
}
I’m curious to know how long it took more seasoned coders to feel like their code was becoming more efficient or elegant.
Edit: It actually seems like my last function doesn’t work with singular arrays, only the batch ones! Would love any advice on what could be wrong there!
I have the same question about time and my code also seems inefficient ! I am going down the line of being confident with what I am currently creating and then work on making it more concise
Hi! What about my code?) If you check it, I will be happy.
function validateCred(card) {
let digits = card.slice();
for (let i = digits.length - 2; i >= 0; i -= 2) {
digits[i] *= 2;
if (digits[i] > 9) {
digits[i] -= 9;
}
}
const sum = digits.reduce((acc, curr) => acc + curr, 0);
return sum % 10 === 0;
}
function findInvalidCards(cardArray) {
const invalidCards = ;
for (let card of cardArray) {
if (!validateCred(card)) {
invalidCards.push(card);
}
}
return invalidCards;
}
function idInvalidCardCompanies(invalidCards) {
const uniqueCompanies = {};
for (let card of invalidCards) {
let firstDigit = card[0];
switch (firstDigit) {
case 3:
if (!uniqueCompanies.includes('Amex')) {
uniqueCompanies.push('Amex');
}
break;
case 4:
if (!uniqueCompanies.includes('Visa')) {
uniqueCompanies.push('Visa');
}
break;
case 5:
if (!uniqueCompanies.includes('Mastercard')) {
uniqueCompanies.push('Mastercard');
}
break;
case 6:
if (!uniqueCompanies.includes('Discover')) {
uniqueCompanies.push('Discover');
}
break;
default:
console.log("Company not found");
}
}
return uniqueCompanies;
}
Hello All,
This is still very much a work in progress. I am still trying to make sure I understand what I actually coded. Any feedback would be greatly appreciated.
Codepen has been updated. It should match the below Gist
Hey @jwl298,
Take a look at the codepen I posted Credit Card Checker Challenge Project (JavaScript) - #1461 by tiercen82
I think I am doing what you are trying to do. I found that if you pass it a single array you have to turn it into a nested array first before processing it. I also tried to make it so that idInvalidCardCompanies() could handle the return from validateCred(). So my validateCred() returns the checked array. I also modified it to accept a number or a string in addition to an array.
I created a workspace so I can continue to refine. I would love your feedback.
https://www.codecademy.com/workspaces/65d7c065c1a4b16c3b60327e
I am not able to edit the existing post anymore. So here is the new codpen
Hi, Thank you for your learning materials. Just wanna share my answer here! Would be cool if you can check my code and lemme know your thoughts. Happy coding guys!
This is my credit card checker, happy to hear your thoughts.
I like your use of filter, much neater than mine
This is my solution for this challenge. i had some problems with the card invalid4 since it isn’t taken as an invalid card. If some one can help me here is my code, i tried using some console.logs() but i dont understand why i dont get it.
I found out this when i get to the last task of making the idInvalidCardCompanies() function where i only get [ 'Mastercard', 'Amex', 'Visa' ]
and i dont get the Discovery one that was missing.
to reduce by 1 the correct syntax is i-- not i-