#40 - Credit Card Mask Kata (7 kyu)

cesar__dlr

Cesar Del rio

Posted on April 6, 2022

#40 - Credit Card Mask Kata (7 kyu)

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!"
Enter fullscreen mode Exit fullscreen mode

My solution:

function maskify(cc) {
    return cc.split("").map((x,i) => (i<cc.length-4)?x="#":x).join("");
}
Enter fullscreen mode Exit fullscreen mode

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? 👇🤔

My Github
My twitter
Solve this Kata

💖 💪 🙅 🚩
cesar__dlr
Cesar Del rio

Posted on April 6, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related