JS: Mask all except the last four digits

This is from an exercise for in-between that I fished out of the net. The aim is to mask all numbers with an X or an asterisk, similar to credit card entries.

The personal solution is waiting in the detailed view of the article.

Example Number: 123456789

Result: 12345****


My personal solution (especially the text in blue):

const number = '12345678999' // our initial number


// This function masks all numbers except the last four.
function maskNumber(number) {

  number = "x".repeat(number.length-4) + number.substring(number.length-4);
  return number;
}

numberModified=maskNumber(number);




// Log to console
console.log("original: " + number);
console.log("modified: " + numberModified);


It's not something that requires a lot of brain power, but it's good as a little exercise for budding programmers or as an in-between exercise.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.