Credit Card Checker - Unerstanding the Provided Solution

Hi!
My question is regarding the last question of the Credit Card Checkers challenge (https://www.codecademy.com/practice/projects/credit-card-checker).

I am super new to coding so I am EVEN struggling to understand the steps in the provided solution for this challenge. Especially regarding the question where one needs to identify the credit card companies that have possibly issued these faulty numbers.

I understand the general idea, however, when looking a snipet of this code block, I don’t really understand the logic behind the if statements that are equal to -1:

function idInvalidCardCompanies(invalidBatch) {
  const companies = [];
  for (let i = 0; i < invalidBatch.length; i++) {
    switch (invalidBatch[i][0]) {
      case 3:
        if (companies.indexOf('Amex') === -1) {
          companies.push('Amex');
        }
        break

Any help is highly appreciated!

I am also open to any suggestions regarding how to better get along with JS language. I am doing the web development career path and everything seems clear until I start doing the challenges. I am not sure if the difficulty level is 10x higher or am just really not understanding the concepts.

Thank you very much in advance!

Hi there, welcome to the forums.

The answer to your question lies in the documentation of the array.prototype.indexOf() method.

You only need to add “Amex”, in this example, to the array of companies which have issued dud card numbers if it’s not there already.

This is the purpose of the if statement. As per the documentation, .indexOf(element) returns -1 if the element value isn’t already present.

So, if we get a value of -1 we know we’ve not already made a note of that card issuer providing faulty numbers, so we add them to our naughty list. :slight_smile:

Does that make sense?