JavaScript 🐲 challenges_4 βš”οΈ

mahmoudessam

Mahmoud EL-kariouny

Posted on March 30, 2023

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

πŸ’– πŸ’ͺ πŸ™… 🚩
mahmoudessam
Mahmoud EL-kariouny

Posted on March 30, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

AdventJS 2023: Day 10 Challenge
javascript AdventJS 2023: Day 10 Challenge

December 13, 2023

AdventJS 2023: Day 8 Challenge
javascript AdventJS 2023: Day 8 Challenge

December 10, 2023

JS Solutions for CodeWars Katas
javascript JS Solutions for CodeWars Katas

May 22, 2022