Hi,
I encounter a problem with step 5 of the project.
Instructions: After finding all the invalid credit card numbers, it’s also necessary to identify the credit card companies that have possibly issued these faulty numbers. Create a function, idInvalidCardCompanies()
that has one parameter for a nested array of invalid numbers and returns an array of companies.
To explain my code below: I went with a nested for loop, which I realise now was probably the best bet in terms of simplicity, and my method to make sure that the ‘faulty’ company name only appears once in the returned array is different than what the hint suggests: for some reason I found it more logical to create counter variables for each company, and when their values returned truthy it would add the name of that company to the array.
Now, the program technically “works”, as in it doesn’t throw any error or weird behaviour, but I’m having troubles making it do part of the job correctly: iterating through the first element of each inner array only, instead of all the elements. Or at least, targeting the first element only.
Currently, it checks if the numbers 3, 4, 5 or 6 are in each array regardless of the index, which results in the returned array being bound to be wrong if we were to work with different arrays (as in this project, all the companies are ‘faulty’ since all the invalid cards arrays start with either one of the 4 digits - so my result only looks correct, but if you were to remove all the arrays starting with 3 for instance, it would still include Amex in the returned array… because the loop checks the full array of invalid numbers, where 3 appears again multiple times).
This is my code (for step 5 only) with some comments and console.log to explain what happens:
const idInvalidCardCompanies = invalidCardsArray => {
let invalidCompanies = [];
let amexCounter = 0;
let visaCounter = 0;
let mastercardCounter = 0;
let discoverCounter = 0;
for (let i = 0; i < invalidCardsArray.length; i++) //iterates through each nested array
{
for (let j = 0; j < invalidCardsArray[i].length; j++) //iterates through the elements of the inner arrays
{
//console.log(invalidCardsArray[i][0]); outputs all the first numbers of each inner array, 15-16 times each
if (invalidCardsArray[i][j] === 3) {
amexCounter++;
}
if (invalidCardsArray[i][j] === 4) {
visaCounter++;
}
if (invalidCardsArray[i][j] === 5) {
mastercardCounter++;
}
if (invalidCardsArray[i][j] === 6) {
discoverCounter++;
}
}
}
//console.log(amexCounter); // outputs 14
//console.log(visaCounter); // outputs 12
//console.log(mastercardCounter); // outputs 12
//console.log(discoverCounter); // outputs 10
amexCounter ? invalidCompanies.unshift('Amex') : console.log("Company not found");
visaCounter ? invalidCompanies.unshift('Visa') : console.log("Company not found");
mastercardCounter ? invalidCompanies.unshift('Mastercard') : console.log("Company not found");
discoverCounter ? invalidCompanies.unshift('Discover') : console.log("Company not found");
console.log ('These companies mailed out cards with invalid numbers: ' + invalidCompanies.join());
}
idInvalidCardCompanies(findInvalidCards(batch)); // Returns the array containing the 4 companies
So what I would need help with is: how do I make the loop iterate over the first element of each ‘inner’ array only, and all of them? Or, if that’s not possible, how do I target the first element in the if statement?
I don’t understand that when I do console.log(invalidCardsArray[i][0]);
then only the first elements are logged, but when I try to write it as such in the if
statement (like so: if (invalidCardsArray[i][0] === 3
) and so forth) it just makes the counter variables increase respectively to 30/32/32/35…
I also tried writing it if (invalidCardsArray[i][j][0] === 3)
but then the function logs 4 times ‘Company not found’ and an empty array. Feels like I’m grabbing at straws here.
I don’t know if it’s a syntax issue or a bigger problem. I also can’t find a method that would targets only the first element for this purpose and yet I feel like it’s right under my nose.
I know my solution is probably a bit too complicated, and I’ll probably start over again with a different approach, but if there is any way to make the above work, I would really like to know my hours of Googling didn’t help me so far.
Many thanks to any charitable soul!
EDIT: Ugh, I also just realised that something was not quite right in my function: I wasn’t using the parameter I created nor was I passing an argument to it (pretty big oversight). Corrected that now, it still works and my question remains the same, but what do you know, in the process of correcting that mistake I think I just created my first higher order function!