-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution0074.js
67 lines (48 loc) · 1.72 KB
/
solution0074.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
56
57
58
59
60
61
62
63
64
65
66
67
/*
--------------- 7 Kyu - Credit Card Mask ------------------
Instructions:
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!"
-------------
Sample Tests
describe("maskify", function(){
it("should work for some examples", function(){
Test.assertEquals(maskify('4556364607935616'), '############5616');
Test.assertEquals(maskify('1'), '1');
Test.assertEquals(maskify('11111'), '#1111');
});
});
--------------
Psuedo Code:
-use slice() to separate last four digits of string and the beginning to the last four
-use replace regEx (/./g, '#) on beginning of string
-return both combined
-------------
Lessons Learned
-this could be done more succinctly by placing everything in one statement, without decalring/assigning variables
-
*/
function maskify(cc) {
let hiddenDigits = cc.slice(0, -4)
let lastFour = cc.slice(-4)
return hiddenDigits.replace(/./g, '#') + lastFour;
}
// -----------------------------------------
function maskify(cc) {
cc = cc.split("");
for(var i = 0; i < cc.length - 4; i++){
cc[i] = "#";
}
cc = cc.join("");
return cc
}