Problem of the Day
A new programming or logic puzzle every Mon-Fri

Credit Card Type

To continue on from yesterday's credit card validation we also need a way to send over what type of card the user has entered. Rather than asking the user for the type of card we handle this automagically. Write a function to determine what type of credit card (amex, visa, etc) the user has entered.

Note: part of the challenging is figuring out the formula to how this is done.

Permalink: http://problemotd.com/problem/credit-card-type/

Comments:

  • Anonymous - 10 years, 5 months ago

    I extended my solution for yesterday's problem

    main :: IO ()
    main = do
                putStrLn "Credit Card Number: "
                i <- readLn
                let k = main' i 
                if k
                    then putStrLn $ checkCardTyp $ intToList i
                    else putStrLn "Invalid"
    
    
    checkCardTyp :: [Int] -> String
    checkCardTyp (x:xs) 
                | x == 4 = "Visa"
                | x == 5 && mc xs = "Mastercard"
                | x == 3 && ae xs = "American Express"
                | x == 6 && dis xs = "Discover"
                | x == 3 && dc xs = "Diners Club"
                | (x == 2 && jcb2 xs) || (x == 1 && jcb1 xs) || (x == 3 && jcb3 xs) = "JCB"
                | otherwise = "No Credit Card"
                    where
                        mc (y:ys) = y >= 1 && y <= 5
                        ae (y:ys) = y == 4 || y == 7
                        dis (y:ys) = y == 5 || (y == 0 && dis' ys)
                        dis' (y1:y2:ys) = y1 == 1 && y2 == 1
                        dc (y:ys) = y == 6 || y == 8 || (y == 0 && dc' ys)
                        dc' (y:ys) = y >= 0 && y <= 5
                        jcb1 (y1:y2:y3:ys) = y1 == 8 && y2 == 0 && y3 == 0
                        jcb2 (y1:y2:y3:ys) = y1 == 1 && y2 == 3 && y3 == 1
                        jcb3 (y:ys) = y == 5
    

    where the main' is the main function of yesterday (but this time it takes only 1 number (full credit card number))

    i tested it with my credit card and it worked... maybe there are some typos, but these are the rules i found on the net

    reply permalink

  • bumbleguppy - 10 years, 5 months ago

    javascript

    function creditCardType(num){
        var regxp;
        var creditCardTypes =   [['^4[0-9]{6,}$','Visa'],
                                ['^5[1-5][0-9]{5,}$','MasterCard'],
                                ['^3[47][0-9]{5,}$', 'American Express'],
                                ['^3(?:0[0-5]|[68][0-9])[0-9]{4,}$', 'Diners Club'],
                                ['^6(?:011|5[0-9]{2})[0-9]{3,}$', 'Discover'],
                                ['^(?:2131|1800|35[0-9]{3})[0-9]{3,}$', 'JCB']];
    
        num = num.toString().replace(/\D/, '');
    
        for(var i = 0, len = creditCardTypes.length; i < len; i++){
            regxp = new RegExp(creditCardTypes[i][0]);
            if(regxp.test(num)){
                return creditCardTypes[i][1];
            }
        }
    
        return 'Unknown';
    }
    

    reply permalink

Content curated by @MaxBurstein