Instructions
Task
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!"
My solution:
function maskify(cc) {
return cc.split("").map((x,i) => (i<cc.length-4)?x="#":x).join("");
}
Explanation
First I splitted into an array the string, then I mapped it and in every iteration I checked if the index of the element being iterated isn't smaller than the length of the string minus 4, if it isn't small it means that it hasn't reach the last 4 characters, so I change the number for a "#", but if it has reached the last 4 characters I just leave them as they are, and at the end I just join the array.
What do you think about this solution? ππ€
Top comments (0)