DEV Community

Cover image for JavaScript ๐Ÿฒ challenges_4 โš”๏ธ
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

JavaScript ๐Ÿฒ challenges_4 โš”๏ธ

Credit Card Mask

  • Usually when you buy something, you're asked whether your credit card number, phone number or answer to your most secret question is still correct.
  • However, since someone could look over your shoulder, you don't want that shown on your screen. Instead, we mask it.
  • Your task is to write a function maskify, which changes all but the last four characters into '#'.

Example:

"4556364607935616"      --> "############5616"
"64607935616"           -->      "#######5616"
"1"                     -->                "1"
""                      -->                 ""

"What was the name of your first pet?"

"Skippy"                                    --> "##ippy"
"Nananananananananananananananana Batman!"  --> ####################################man!"
Enter fullscreen mode Exit fullscreen mode

Task URL

My solution:

function maskify(cc) {

    const creditCardNumber = cc.toString();

    if (creditCardNumber.length === 1 || creditCardNumber.length === 4) return cc;
    if (creditCardNumber === "") return "";

    let start = creditCardNumber.slice(0, -4);
    let end = creditCardNumber.slice(-4);
    let hash = '#';

    let hashedCreditCardNumber = hash.repeat(start.length).concat(end);

    return hashedCreditCardNumber;
}
Enter fullscreen mode Exit fullscreen mode

Code Snapshot:

Image description

10 JavaScript Games to learn-Totally Cool & Fun ๐Ÿš€โœจ๐Ÿ”ฅ

๐ŸŽฅ

Connect with Me ๐Ÿ˜Š

๐Ÿ”— Links

linkedin

twitter

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited
const maskify = (cc, [a,b,c,d,e] = [...cc].reverse()) => 
 [a,b,c,d,e ? '#'.repeat(cc.length-4) : ''].reverse().join``
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mahmoudessam profile image
Mahmoud EL-kariouny

Thanks for sharing your amazing solution with me. :)