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!"
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;
}
Code Snapshot:
10 JavaScript Games to learn-Totally Cool & Fun ๐โจ๐ฅ
๐ฅ
Top comments (2)
Thanks for sharing your amazing solution with me. :)