Algorithm 3 - [JS] Credit Card Mask

luneah·2021년 12월 4일
0

Algorithm CodeKata

목록 보기
3/13
post-thumbnail

Credit Card Mask

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 :

maskify("4556364607935616") == "############5616"
maskify(     "64607935616") ==      "#######5616"
maskify(               "1") ==                "1"
maskify(                "") ==                 ""

// "What was the name of your first pet?"
maskify("Skippy")                                   == "##ippy"
maskify("Nananananananananananananananana Batman!") == "####################################man!"

📌 Needs ) 마지막 4개 문자를 제외한 모든 문자를 '#'으로 변경하는 함수 maskify를 작성하라.

📁 Sol ) slice와 replace 메소드 사용

function maskify(cc) {
  return cc.slice(0, -4).replace(/./g, '#') + cc.slice(-4);
}

💡 Other ways to solve ) repeat와 Math method 사용

maskify = (cc) => '#'.repeat(Math.max(0, cc.length - 4)) + cc.substr(-4);
profile
하늘이의 개발 일기

0개의 댓글