Challenge Project: Credit Card Checker

Hi, everyone! I’m in trouble I just started learning and I’m trying to solve simple code with first array I don’t know what the sum is working wrong. Please leave comments here.

// 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 validateCreditCard = (arr1) => { let sum = 0; for(let i = arr1.length-1; i >= 0; i--) { let numbers = arr1[i]; if (i%2 === 0) { arr1[i] *= 2; if(arr1[i] > 9) { arr1[i] -= 9 } } else { sum += numbers console.log(sum) } } } validateCreditCard(valid1)

why is
sum += numbers
inside an else block?

Ohh. I think it is wrong. I’ll review. Thank you.

Please what is the wrong? I checked one array, it is ok. but replaced with batch, got error, I added two different Codebyte. I cannot imagine what do I do this error, please help a little, I would appreciate.

// 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 validateCreditCard = (arr) => { let sum = 0; for(let i = arr.length-1; i >= 0; i--) { if (i%2 === 0) { arr[i] = arr[i] * 2; if(arr[i] > 9) { arr[i] = arr[i] - 9; } } sum = sum + arr[i]; } console.log(sum); if(sum % 10 === 0) { console.log('valid'); } else { console.log('invalid'); } } validateCreditCard(valid1)
// 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 validateCreditCard = (arr) => { let sum = 0; for(let i = arr.length-1; i >= 0; i--) { if (i%2 === 0) { arr[i] = arr[i] * 2; if(arr[i] > 9) { arr[i] = arr[i] - 9; } } sum = sum + arr[i]; } console.log(sum); if(sum % 10 === 0) { console.log('valid'); } else { console.log('invalid'); } } validateCreditCard(batch)

Because batch is an array containing arrays you need to iterate through batch and then call ValidateCreditCard() on each item in the batch array.

Hope that helps!

Thank you for response, how to iterate the batch? Should I change arr to batch as argument, right?

Maybe something like:

for (let i = 0; i < batch.length; i++) {
  validateCreditCard(batch[i]);
}
2 Likes

Hi, everyone! Hope I completed the project, compared with https://discuss.codecademy.com/t/credit-card-checker-challenge-project-javascript/462375?u=artashes85, and corrected some errors, but there are little different results, please review if there are errors. Thanks!

// 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]; // Add your functions below: const validateCred = (arr) => { // // console.log(arr); // let sum = 0; for(let i = arr.length - 1; i >= 0; i--) { // // console.log(arr[i]); // let currNumber = arr[i]; if (i%2 === 0) { currNumber *= 2; // // console.log(arr[i]); // console.log(currNumber) // break; // if(currNumber > 9) { currNumber -= 9; } } // console.log(currNumber) sum += currNumber; } // console.log(sum); if(sum % 10 === 0) { return true } else { return false } } // validateCred(valid1); // Test functions: console.log(validateCred(valid1)); // Should print true console.log(validateCred(invalid1)); // Should print false const findInvalidCards = cardNumbers => { const invalid = []; for (let i = 0; i < cardNumbers.length; i++) { let currCred = cardNumbers[i]; if (!validateCred(currCred)) { invalid.push(currCred); } } return invalid; } // Test function console.log(findInvalidCards([valid1, valid2, valid3, valid4, valid5]));// Shouldn't print anything console.log(findInvalidCards([invalid1, invalid2, invalid3, invalid4, invalid5])); // Should print all of the numbers console.log(findInvalidCards(batch)); // Test what the mystery numbers are const 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; case 4: if (companies.indexOf('Visa') === -1) { companies.push('Visa'); } break; case 5: if (companies.indexOf('Mastercard') === -1) { companies.push('Mastercard'); } break; case 6: if (companies.indexOf('Discover') === -1) { companies.push('Discover'); } break; default: console.log('Company not found'); } } return companies; } console.log(idInvalidCardCompanies([invalid1])); // Should print['visa'] console.log(idInvalidCardCompanies([invalid2])); // Should print ['mastercard'] console.log(idInvalidCardCompanies(batch)); // Find out which companies have mailed out invalid cards
1 Like

Looks good overall.

I think that your stuff gives the correct result for arrays that have an even length, but not for arrays an odd length.
(Your does the doubling of the digit if the index is even, which matches “double every other digit going backward starting with the next-to-last digit” in the algorithm.)
You could fix that by checking whether the difference between the length and the index is even, instead of checking if the index is even.

1 Like

On the 38 line, I changed to if(arr.length - 1 === 0) or if(i%2 != 0), it didn’t work correctly, should been true and false, didn’t print empty array, that is the wrong, I stressed? Please, write your example for this 38 line. I don’t imagine how odds works, if there is material or simple example, it will be helped me to understand.

// 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]; // Add your functions below: const validateCred = (arr) => { // // console.log(arr); // let sum = 0; for(let i = arr.length - 1; i >= 0; i--) { // // console.log(arr[i]); // let currNumber = arr[i]; if (i % 2 != 0) { currNumber *= 2; // // console.log(arr[i]); // console.log(currNumber) // break; // if(currNumber > 9) { currNumber -= 9; } } // console.log(currNumber) sum += currNumber; } // console.log(sum); if(sum % 10 === 0) { return true } else { return false } } // validateCred(valid1); // Test functions: console.log(validateCred(valid1)); // Should print true console.log(validateCred(invalid1)); // Should print false const findInvalidCards = cardNumbers => { const invalid = []; for (let i = 0; i < cardNumbers.length; i++) { let currCred = cardNumbers[i]; if (!validateCred(currCred)) { invalid.push(currCred); } } return invalid; } // Test function console.log(findInvalidCards([valid1, valid2, valid3, valid4, valid5]));// Shouldn't print anything console.log(findInvalidCards([invalid1, invalid2, invalid3, invalid4, invalid5])); // Should print all of the numbers console.log(findInvalidCards(batch)); // Test what the mystery numbers are const 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; case 4: if (companies.indexOf('Visa') === -1) { companies.push('Visa'); } break; case 5: if (companies.indexOf('Mastercard') === -1) { companies.push('Mastercard'); } break; case 6: if (companies.indexOf('Discover') === -1) { companies.push('Discover'); } break; default: console.log('Company not found'); } } return companies; } console.log(idInvalidCardCompanies([invalid1])); // Should print['visa'] console.log(idInvalidCardCompanies([invalid2])); // Should print ['mastercard'] console.log(idInvalidCardCompanies(batch)); // Find out which companies have mailed out invalid cards

One approach is: to check whether the difference between the length and the index is even
if ((arr.length - i) % 2 === 0) {

other approach

A different way to accomplish that would be to have a variable that counts from the end of the array.

const validateCred = (arr) => {
    let sum = 0;
    let j = 0; // variable to count from the end
    for(let i = arr.length - 1; i >= 0; i--) {
        let currNumber = arr[i];
        if (j % 2 != 0) {
            currNumber *= 2;
            if(currNumber > 9) {
                currNumber -= 9;
            } 
        } 
        sum += currNumber;
        j++;
    } 
    if(sum % 10 === 0) {
        return true
    } else {
        return false
    }
};

Thanks you. Works. Can you explain ((arr.length - i) % 2 === 0) how it works? It will help me understand logically.
Correct? I imagine like
16-0=16 % 2 === 0
16-1=15 % 2 === 0
16-2=14 % 2 === 0
16-3=13 % 2 === 0
16-4=12 % 2 === 0

// 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]; // Add your functions below: const validateCred = (arr) => { // // console.log(arr); // let sum = 0; for(let i = arr.length - 1; i >= 0; i--) { // // console.log(arr[i]); // let currNumber = arr[i]; if ((arr.length - i) % 2 === 0) { currNumber *= 2; // // console.log(arr[i]); // console.log(currNumber) // break; // if(currNumber > 9) { currNumber -= 9; } } // console.log(currNumber) sum += currNumber; } // console.log(sum); if(sum % 10 === 0) { return true } else { return false } } // validateCred(valid1); // Test functions: console.log(validateCred(valid1)); // Should print true console.log(validateCred(invalid1)); // Should print false const findInvalidCards = cardNumbers => { const invalid = []; for (let i = 0; i < cardNumbers.length; i++) { let currCred = cardNumbers[i]; if (!validateCred(currCred)) { invalid.push(currCred); } } return invalid; } // Test function console.log(findInvalidCards([valid1, valid2, valid3, valid4, valid5]));// Shouldn't print anything console.log(findInvalidCards([invalid1, invalid2, invalid3, invalid4, invalid5])); // Should print all of the numbers console.log(findInvalidCards(batch)); // Test what the mystery numbers are const 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; case 4: if (companies.indexOf('Visa') === -1) { companies.push('Visa'); } break; case 5: if (companies.indexOf('Mastercard') === -1) { companies.push('Mastercard'); } break; case 6: if (companies.indexOf('Discover') === -1) { companies.push('Discover'); } break; default: console.log('Company not found'); } } return companies; } console.log(idInvalidCardCompanies([invalid1])); // Should print['visa'] console.log(idInvalidCardCompanies([invalid2])); // Should print ['mastercard'] console.log(idInvalidCardCompanies(batch)); // Find out which companies have mailed out invalid cards

Hi Artashes,

I completed the project in a very similar way, thank you for sharing this :slight_smile:. It’s always nice to confirm that I am on the right track.

Mike

Hi, nice. Please share the code with us

No problem, here is my completed solution.

// 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]; // Add your functions below: const checkCard = (str) => { const cardArray = str.split(''); const isValid = validateCred(cardArray); return isValid ? 'The card is valid' : 'The card is not valid'; }; // validate credit card using Luhn Algorithm const validateCred = (arr) => { let sum = 0; let isSecond = false; // loop in reverse(right to left) for (let i = arr.length - 1; i >= 0; i--) { //assign current index to digit let digit = parseInt(arr[i]); // Luhn's law: // if the digit is the second, double it. // if it is greater than 9 take 9 away if (isSecond) { digit *= 2; if (digit > 9) { digit -= 9; } } // add computed digit to the sum sum += digit; // toggle isSecond boolean isSecond = !isSecond; } // if the sum divided by 10 has a remainder of zero, it is valid // else, it is false. return sum % 10 === 0; }; const findInvalidCards = (nestedArr) => { const invalidCards = []; for (let i = 0; i < nestedArr.length; i++) { const isValid = validateCred(nestedArr[i]); if (!isValid) { invalidCards.push(nestedArr[i]); } } return invalidCards; }; const idInvalidCardCompanies = (nestedArr) => { const cardCompanies = []; let digit = 0; for (let i = 0; i < nestedArr.length; i++) { digit = nestedArr[i][0]; switch (digit) { case 3: if (!cardCompanies.includes(`Amex (American Express)`)) { cardCompanies.push(`Amex (American Express)`); } break; case 4: if (!cardCompanies.includes('Visa')) { cardCompanies.push('Visa'); } break; case 5: if (!cardCompanies.includes('Master Card')) { cardCompanies.push('Master Card'); } break; case 6: if (!cardCompanies.includes('Discover')) { cardCompanies.push('Discover'); } break; default: console.log('Company not found for card starting with: ' + digit + '.'); break; } } return cardCompanies; }; const invalidCards = findInvalidCards(batch); const cardCompanies = idInvalidCardCompanies(invalidCards); // display nested array with invalid card numbers invalidCards.forEach((card) => { console.log(card); }); // display found companies for invalid cards cardCompanies.forEach((company) => { console.log(company); }); // Extra Credit //const result = checkCard('3532759594973662');

Finally finished, spent the entire day debugging and writing out comments.
Check it out here: projects/codecademy-assignments/JS_assignments/projects/credit-card-checker-starter/main.js at main · kiddulu916/projects · GitHub
Did both bonus challenges also, bonus challenge 1 starts on line 146 and bonus challenge 2 starts on line 194.
Let me know what yall think, how it can be improved. Added way more comments then I usually do but trying to get in the habit of it more. Let me know if its to many, lol.