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

1 Like

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

typical solution

Hello There! Given that we won’t be working with big datasets I made it my way with the methods and for functions that we saw in the course:

// Add your functions below:
function validateCred(arr) {                //checks if it's a valid credit card number (regardless the length)
    let sum = 0;
    let lastDigit = 0;
    let double = true;
    for (i = arr.length -2; i >= 0; i--){   //loops throug the array backwards avoiding the last digit.
        if(double){                         //checks if it has to duplicate the value.
            for (const num of String(arr[i] *2).split('')){sum += Number(num);}; //adds the digits
        } else {sum += arr[i];}             //adds directly the array value
        double = !double;                   //toggles between duplicate the value or not
    };
    lastDigit = (10 - (sum %10)) % 10;      //calculates te verifier digit
    return arr[arr.length-1] === lastDigit? true: false; //if last digits equals verifier returns ok
};

function findInvalidCards(nArr){            //returns invalid credit cards in a batch
    let iCards = [];
    for (const arr of nArr){if (!validateCred(arr)) {iCards.push(arr);};}  
    return iCards;
};

function idInvalidCardCompanies(nArr){      //returns the company for each credit card in a batch
    let companies = [];
    for (const arr of nArr){                //--can be changed to an 'i = 0' loop for big datasets.
        let company = '';                   //--can be avoided in big datasets checking directly in each case.
        switch (arr[0]) {                   //checks the first number in the array.
            case 3: company ='Amex'; break;
            case 4: company ='Visa'; break;
            case 5: company ='Mastercard'; break;
            case 6: company ='Discover'; break;
            default: company ='Company not found'; 
        };
        if (!companies.some(c => c === company)){companies.push(company);} //push the company if isn't in the list.
    }
    return companies;
}

See ya!

I’ve also finished the optional requirements:

// Add your functions below:
function validateCred(arr){return arr[arr.length-1] === verNumber(arr)? true: false;} //checks the verificator number.

function verNumber(arr) {                   //calculates the verificator number (last number)
    let sum = 0;
    let double = true;
    for (i = arr.length -2; i >= 0; i--){   //loops throug the array backwards avoiding the last digit.
        if(double){                         //checks if it has to duplicate the value.
            for (const num of String(arr[i] *2).split('')){sum += Number(num);}; //adds the digits
        } else {sum += arr[i];}             //adds directly the array value
        double = !double;                   //toggles between duplicate the value or not
    };
    return (10 - (sum %10)) % 10;           //returns te verifier digit
};

function findInvalidCards(nArr){            //returns invalid credit cards in a batch
    let iCards = [];
    for (const arr of nArr){if (!validateCred(arr)) {iCards.push(arr);};}   //push the invalid cards
    return iCards;
};

function idInvalidCardCompanies(nArr){      //returns the company for each credit card in a batch
    let companies = [];
    for (const arr of nArr){                //--can be changed to an 'i = 0' loop for big datasets.
        let company = '';                   //--can be avoided in big datasets checking directly in each case.
        switch (Number(arr[0])) {                   //checks the first number in the array.
            case 3: company ='Amex'; break;
            case 4: company ='Visa'; break;
            case 5: company ='Mastercard'; break;
            case 6: company ='Discover'; break;
            default: company ='Company not found'; 
        };
        if (!companies.some(c => c === company)){companies.push(company);} //push the company if isn't in the list.
    }
    return companies;
}

function ccNumberToArr (cc){                //can receive also numeric values to work with.
    return typeof(cc) === 'string'? cc.split(''): String(cc).split('');
}

function batchToArr (ccArr){                //converts an array of credit card numbers as string/numeric values
    let retArr = [];
    for (const cc of ccArr){                //iterates throug each cc number
        retArr.push(ccNumberToArr(cc));     //push the cc array in the batch
    }
    return retArr;
}

function allwaysValid(nArr){
    let toFix = [...nArr];
    let ret = [];
    let ver = '';
    for (const arr of toFix){
        ver = String(verNumber(arr));
        if (arr[arr.length-1] !== ver){arr[arr.length - 1] = ver;}
        ret.push(arr.join(''));             //converts the array to string (easier to check in console);
    }
    return ret;
}

const listFromWebsite = [                   //https://www.freeformatter.com/credit-card-number-generator-validator.html
    4532288583949680, 4929029336020473, '4767662175844952795',
    5402517799555269, 2720991854242703, 5111254865967291,
    341935796884992,  377705550885353,  340443268883676,
    6011997155955579, 6011497024483424, '6011539395646078910'
];

console.log(idInvalidCardCompanies(findInvalidCards(batchToArr(listFromWebsite))));
console.log(allwaysValid(batchToArr(listFromWebsite)));
//console.log(allwaysValid(batch));

Let me know if I can improve! :smiley: Happy Coding!

I am honestly quite proud of myself as it took me only about an hour to complete this. YAY :smiley:
Debugging within vscode saves my life.

My code: GitHub - enzodia1908/credit-card-checker: Credit Card Checker JavaScript Project

My solution, including the bonus challenges (adding new cards & fixing all invalid numbers)

1 Like

My solution, havent’t got to the bonus challenges just yet! Be good to know what people think of it :slight_smile:

Credit Card Checker
Github

My solution :upside_down_face:

Github

1 Like

Credit Card Checker

Hi Everyone! Here’s my approach to the challenge:

I used a Set in order to handle the duplicates in the last step, would love to hear your feedback!
Edu

This one was super interesting. I can see how this will translate well into we development. Javascript seems quite powerful. Let me know what you guys think!

Here is my contribution, including the optional challenging extra functions:

My code!