Credit Card Checker Challenge Project (JavaScript)

Here’s my solution; it may not be elegant, but it works. Feel free to share your thoughts. This challenge was intimidating, at first, but we’ve got this and whatever comes next. Good luck everyone!

const validateCred = arr => {
let oddNums =
let evenNums =
let oddNumSum = 0
let evenNumSum = 0
for (i = arr.length - 2; i >= 0; i -= 2 ) {
oddNums.push((arr[i] * 2))
}
for (k = 0; k < oddNums.length; k++) {
if (oddNums[k] > 9) {
oddNums[k] = oddNums[k] - 9
}
}

for (l = 0; l < oddNums.length; l++) {
    oddNumSum += oddNums[l]
}

for (j = arr.length - 1; j >= 0; j -= 2) {
evenNums.push(arr[j])
}
for (m = 0; m < evenNums.length; m++) {
evenNumSum += evenNums[m]
}

if ((evenNumSum + oddNumSum) % 10 === 0) {
    return true
} else {
    return false
}

}

let invalidList =
const findInvalidCards = nest => {

for (n = 0; n < nest.length; n++) {
    if (validateCred(nest[n]) === false) {
    invalidList.push(nest[n])
   }
}
return invalidList

}

findInvalidCards(batch)
console.log(invalidList)

let invalidListNames =
const idInvalidCardCompanies = invalidArr => {
for (o = 0; o < invalidArr.length; o++) {
if (invalidArr[o][0] === 3) {
invalidListNames.push(‘Amex’)
}
else if (invalidArr[o][0] === 4) {
invalidListNames.push(‘Visa’)
}
else if (invalidArr[o][0] === 5) {
invalidListNames.push(‘Mastercard’)
}
else if (invalidArr[o][0] === 6) {
invalidListNames.push(‘Discover’)
}
else {
invalidListNames.push(Company not found)
}

}
return console.log(invalidListNames.filter((value, index) => invalidListNames.indexOf(value) === index))

}

idInvalidCardCompanies(invalidList)

Hi,

Could someone clarify the meaning of the third part of task 7?

  • Create a function that will convert invalid numbers into valid numbers.

If the number is not a valid number, how should I know what its valid counterpart would be?

Here is my code so far:

Thanks!

this code same with chatGPT suggest to me for improvement…

u need to makesure your number sum is 100…if not that card is invalid

1 Like

https://github.com/mgo26/credit-card-checker-starter/blob/main/main.js

Here is my code, done a lot of coding in Python over the last year or two but this is my first proper delve into JS, get the feeling my code is pretty horrible but would love some feedback!!

Thanks in advance!

// 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 = (array) => {
    total = array[array.length - 1];
    for (i = array.length - 2; i >= 0; i-=2) {
        if ((array[i] * 2) > 9) {
            total += (array[i] * 2) - 9;
        } else {
            total += array[i] * 2;
        }
    }
    for (i = array.length - 3; i >= 0; i-=2) {
        total += array[i]
    }
    return total % 10 === 0;
};

console.log(validateCred(valid1));
console.log(validateCred(invalid1));

const findInvalidCards = (nestArray) => {
    const invalidBatch = [];
    for (const array of nestArray) {
        if (validateCred(array) === false) {
            console.log(array);
            invalidBatch.push(array);
        }
    }
    return invalidBatch;
};

const dodgyCards = findInvalidCards(batch);

const idInvalidCardCompanies = (nestArray) => {
    companyArray = [];
    for (const array of nestArray) {
        if (array[0] === 3) {
            if (companyArray.includes('American Express')) {
                continue;
            } else {
                companyArray.push('American Express');
            }
        } else if (array[0] === 4) {
            if (companyArray.includes('Visa')) {
                continue;
            } else companyArray.push('Visa');
        } else if (array[0] === 5) {
            if (companyArray.includes('Mastercard')) {
                continue;
            } else companyArray.push('Mastercard');
        } else if (array[0] === 6) {
            if (companyArray.includes('Discover')) {
                continue;
            } else companyArray.push('Discover');
        } else {
            console.log('Company not found');
        }
    } 
    return companyArray;
}

console.log(idInvalidCardCompanies(dodgyCards));

Hi all :smiley:

Here is my code for the Credit Card Checker Challenge: GitHub Code

I have found JS challenging so far and practiced a lot. Probably not the best and most concise, but it works (without checking solutions). :stuck_out_tongue_winking_eye:

I’d love to hear from you! :incoming_envelope:

Kind regards,
Arri

Thank you for all your feedback! I will look into that

typical solution