-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskify.js
37 lines (29 loc) · 1018 Bytes
/
maskify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*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 '#'.
Examples
"4556364607935616" --> "############5616"
"64607935616" --> "#######5616"
"1" --> "1"
"" --> ""
// "What was the name of your first pet?"
"Skippy" --> "##ippy"
"Nananananananananananananananana Batman!"
-->
"####################################man!"
*/
/**
*
* @param {String} cc
* @return {String}
*/
function maskify(cc) {
if (cc.length <= 4) {
return cc;
}
const ccLength = cc.slice(0, cc.length - 4).length;
return `${'#'.repeat(ccLength)}${cc.slice(cc.length - 4)}`;
}
console.log(maskify('4556364607935616'));
console.log(maskify('5616'));