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

Latest 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. :)