[05.06.22] Coding test

Juyeon.it·2022년 5월 6일
0

Coding test

목록 보기
25/32

Credit Card Mask

Description

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"
"" --> ""

My answer

function maskify(cc) {
  if (cc.length < 5) { return cc };
  return '#'.repeat(cc.slice(0, -4).length) + cc.slice(-4);
}

Other solutions

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

0개의 댓글

관련 채용 정보