Drill

ProblemsTypeScript › payments

Mask a card number

easypaymentsTypeScript

Support staff can see the last four digits of a card and nothing else.

maskCard(cardNo: string) → string

Solve it in the editor →

Where you start

function maskCard(cardNo: string): string {
  
}

Worked examples

CallResult
maskCard("4506349012345678")"************5678"
maskCard("12345")"*2345"
maskCard("1234")"1234"
maskCard("")""

Hint

Build the asterisks from the length, then glue on the tail.

Reference solution in TypeScript
function maskCard(cardNo: string): string {
  if (cardNo.length <= 4) return cardNo;
  return '*'.repeat(cardNo.length - 4) + cardNo.slice(-4);
}

The same problem in another language

More payments problems in TypeScript