return breaks of the whole function, but do you want that when iterating through a whole array? In this case the first time it meets a card that is not of a company it will stop doing what it did.
Instead you would want it pushed to the same array where you store the company names.
Oh yeah. Thanks for that @janneslohmeijer. And yeah, batch is made out of arrays. The trouble I’m having right now is, comparing the first digit of the element to that of the credit card companies. I’m unsure of what built in objects I’m able to use. These are what I have so far.
const validateCred = card => {
const arr = [];
for (i = 0; i < card.length; i++) {
if (i % 2 === 0) {
if (card[i]*2 < 10) {
arr.push(card[i]*2);
} else {
arr.push(card[i]*2-9);
}
} else {
arr.push(parseInt(card[i], 10));
}
}
return arr.reduce( (prv, cur) => prv + cur) % 10 === 0;
}
console.log(validateCred(valid3));
let validCred = [];
let invalidCred = [];
const findInvalidCards = invlCards => {
for (j = 0; j < invlCards.length; j++) {
if (validateCred(invlCards[j])) {
validCred.push(invlCards[j]);
} else {
invalidCred.push(invlCards[j]);
}
}
}
findInvalidCards(batch);
console.log(invalidCred);
let companies = [];
const idInvalidCardCompanies = invldCompanies => {
for (k = 0; k < invldCompanies.length; k++) {
if (invldCompanies[k].substring(0, 1) === '3') {
companies.push('Amex (American Express)');
} else if (invldCompanies[k].substring(0, 1) === '4') {
companies.push('Visa');
} else if (invldCompanies[k].substring(0, 1) === '5') {
companies.push('Mastercard');
} else if (invldCompanies[k].substring(0, 1) === '6') {
companies.push('Discover');
} else {
return 'Company not found!';
}
}
}
idInvalidCardCompanies(invalidCred);
console.log(companies);
Hey @janneslohmeijer! I did it! You made me realize that I’m dealing with the array of numbers, so I quickly switched out the strings that I had in the if…else if statement. And as I read more on MDN regarding the “two-dimensional array”, found out that I can access the first digit of the array of numbers! Really appreciate your help a lot
Now I’m just trying to figure out the last part of the exercise telling us that,
“This array should NOT contain duplicates, i.e. even if there are two invalid Visa cards, "Visa" should only appear once in the array.”
I suppose I have to use the .filter() method?
nicely thought out function. can someone explain why taking the modulo 2 for each “i” will never reach the last number(unique digit) in the card.length? Is it because the operator is set to i<card.length ?
Is it correct to assume that no matter the length, the < operator will never reach the last “i” number? Thanks.
Switches have less repeated code in them, but still quite a lot, if one keeps stripping away repeated stuff then it’s looking a whole lot like an object right?
Let’s say an array has a length of 11.
The last index of that is 10, which is 0 mod 2
Or, for an array of length 10, the last index is 1 mod 2
If you’re talking about loops in general (having nothing to do with modular arithmetic) then, what do the different parts in a for-statement do, and what will the final therefore be?
In that case, what do you mean by last number? last number of which numbers?
If you iterate through an array you would probably want to visit each element, including the last one, given that you’re probably counting up from 0, when would you need to stop?