I pretty much managed to do it, but but the idInvalidCardCompanies is not working, could anybody help? Thank you
// 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 batch2 = [ invalid1, invalid2, invalid3, invalid4, invalid5];
let total = 0;
// Add your functions below:
let validCred = (arr) => {
arr.reverse()
for(let i = 0; i < arr.length; i++){ //looping through the array
if(i%2 !== 0){ //using modulus to check every other number in the array
arr[i] * 2 //if true, muktiply number by 2
}
if(arr[i] > 9){ //if number bigger than 9, then substract 9 from it
arr[i] - 9;
}
//console.log(arr[i])
total += arr[i] //add all the total to the total var
}
//console.log(total)
arr.reverse() //reversing array to original position.
if(total % 10 === 0){
return true;
} else {
return false;
}
}
let invalidCC = [];
let findInvalidCards = (arr) => { //function to check for invalid cards, and adding them to the empty array.
arr.forEach(x => {
if(validCred(x) === false){
invalidCC.push(x);
}
})
//console.log(invalidCC)
}
findInvalidCards(batch)
let invalidCompanies = [];
let idInvalidCardCompanies = (arr) => {
for(i=0; i < arr.length; i++){
if(arr[i][0] === 3 && invalidCompanies.indexOf('Amex') === -1) {
invalidCompanies.push('Amex')
} else if(arr[i][0] === 4 && invalidCompanies.indexOf('Visa') === -1){
invalidCompanies.push('Visa')
} else if(arr[i][0] === 5 && invalidCompanies.indexOf('Mastercard') === -1){
invalidCompanies.push('Mastercard')
} else if(arr[i][0] === 6 && invalidCompanies.indexOf('Discover') === -1){
invalidCompanies.push('Discover')
} else {
return 'company not found'
}
}
console.log(invalidCompanies)
}
idInvalidCardCompanies(invalidCC)
I did what you did, but in the end I changed the last else statement to an ‘else if’ and used the following:
else if (arr[i][0] != 3 && arr[i][0] != 4 && arr[i][0] != 5 && arr[i][0] != 6){
console.log('Company not found')
}
because it was console logging ‘company not found’ when it actually should know the company.
Let me know if that helps!
Ah thanks for that, didn;t realise that.
What I was initially struggling with was this line:
if(arr[i][0] === 3 && invalidCompanies.indexOf('Amex') === -1)
So basically checking if card already exists, then don’t add it to the array.
But when I run it, it doesn’t return anything for some reason.
Right! not sure why that doesnt work actually! I used .includes() and set it to only add if it does not value ‘true’ already. that worked for me
else if (arr[i][0] === 6 && invalidCompanies.includes(‘discover’) != true)
There are a few issues that I have found within this code:
-
arr.reverse()
- you are mutating the original array instead of storing a new array to mutate, which is against the constraints of the project, but I originally did this too by mistake
resource: Array.prototype.reverse() - JavaScript | MDN
-
if(i%2 !== 0)
- you are checking the index of all digits, including the check digit which can often be at an even position in the array, so the InvalidCC array often contains valid card numbers (Don’t forget that AMEX cards are 15 digits, which causes an issue with the check digit) -
return 'company not found'
- this will return the function and not evaluate any other element of the array as an example:
ENTER FUNCTION
First Iteration:
First digit of card number = 3
if company not in array
push value of AMEX to array of invalid companies
Second Iteration:
First digit of card number = 3
if company not in array
-> AMEX is already in array due to first iteration
else
return from function with the value of 'company not found'
EXIT FUNCTION
Thankfully I managed to complete the challenge on my own and most of my solutions were fairly similar to CodeAcademy’s solutions except for the last one. I used a set to house the companies that issued invalid cards, which only allowed one occurrence of any value, then i converted the set into an array at the end.
I have completed the task. Some of my code is unorthodox I think; but I’ve completed the task successfully all the same. Please have a look at my code on github
Here’s my solution.
Used the .reduceRight method for the validateCred function.
For findInvalidCards:
const findInvalidCards = cards => cards.filter(card => validateCred(card) === false);
And a series of if statements for the idInvalidCardCompanies function.
That’s because your function is printing the results rather than returning them.
Hello,
I finished my code today.
This is my solution for the Credit Card Checker with the extra features requested (StringToArray() and CorrectCredit())
Alright, here’s my attempt. I think it came out pretty good and very similar to a lot of others.
So many different solutions! Very interesting going through everyone’s code and seeing how different people tackled the same project.
Here’s mine:
In your functions you have them logging the the result to the console. Try replacing the “console.log()” with “return”. That may fix your problem.
I also noticed it and reported the bug, unfortunately now almost 3 years later @codecademy did not do anything with this feedback yet.
Took a while but I nailed it.
Here is the code of my project. I’ll be happy to receive any feedback! GitHub - Juneik/Credit-Card-Checker
Hello mates!
Here’s my version of the project: Github Repo - Credit card checker.
Hoping to see any review about my code. Thanks!.