I got the below coding assessment question in Javascript
. I tried my best to solve but there are few edge cases I missed. I need help to identify those missing cases
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 will:
Mask all digits (0-9) with #, unless they are first or last four characters.
Never mask credit cards with less than 6 characters.
Never mask non-digit characters.
Documentation
maskify(cc)
Parameters:
cc: String
A string of any characters.
Guaranteed Constraints:
The input string will never be null or undefined.
Returns: String
The input string with all but the first and last four characters replaced with ‘#’.
This is what I tried so far
function maskify (cc) {
if (cc.length < 6) {
let reversed = reverse(cc);
let newString = '';
for (let i = 0; i < reversed.length; i++) {
if (i < 4) {
newString += reversed[i];
} else {
newString += '#';
}
}
return reverse(newString);
} else {
return cc.slice(0, -4).replace(/./g, '#') + cc.slice(-4);
}
}
function reverse(str) {
return str.split("").reverse().join("");
}
Output
should mask the digits of standard credit cards
expected '############0694' to equal '5###########0694'
Completed in 2ms
should not mask the digits of short credit cards
expected '#4321' to equal '54321'