-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
credit-card-mask.js
55 lines (49 loc) · 1.18 KB
/
credit-card-mask.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// return masked string
function maskify(cc) {
// a place to store the masked number
let masked = '';
// iterate over the string in reverse
for (let i = cc.length - 1; i >= 0; i--) {
const letter = cc[i];
// if the current index is greater than length - 4
if (i > cc.length - 5) {
// append the letter to the masked number
masked = letter + masked;
} else {
// else
// append a #
masked = '#' + masked;
}
}
// return the masked number
return masked;
}
// return masked string
function maskify(cc) {
// a place to store the masked number
let masked = '';
// iterate over the string in reverse
for (let i = 0; i < cc.length; i++) {
const letter = cc[i];
// if the current index is greater than length - 4
if (i < cc.length - 4) {
// append the letter to the masked number
masked += '#';
} else {
// else
// append a #
masked += letter;
}
}
// return the masked number
return masked;
}
function maskify(cc) {
return cc.split('').map((letter, index, array) => {
if (index < array.length - 4) {
return '#';
} else {
return letter;
}
}).join('');
}