Back to Freecodecamp

Build an Email Masker

curriculum/challenges/english/blocks/lab-email-masker/66b205e6eacba4c4e54ea434.md

latest3.4 KB
Original Source

--description--

In this lab, you will mask the username part of an email address with asterisks. Masking is a term used to hide or replace sensitive information with asterisks or other characters.

For example, if the email address was [email protected], then the masked email address will be m*****[email protected].

Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.

User Stories:

  1. Create a function named maskEmail that takes email as an argument.
  2. Inside the function, you should mask the email and append the domain name to it. Remember that you can use methods like slice, repeat, indexOf or even replace to help you.
  3. Outside the function, declare a variable named email to store the email address you want to mask.
  4. Call the maskEmail function with the email variable and output the result to the console.
  5. maskEmail("[email protected]") should return "a*******[email protected]".
  6. maskEmail("[email protected]") should return "f**********[email protected]".
  7. maskEmail("[email protected]") should return "i**[email protected]".
  8. maskEmail("[email protected]") should return "u**[email protected]".

--hints--

You should define a function named maskEmail.

js
assert.isFunction(maskEmail);

The maskEmail function should take a string, email as an argument.

js
assert.match(maskEmail.toString(), /\s*maskEmail\(\s*\w+\s*\)/);

Outside the function, you should have an email variable.

js
assert.isDefined(email);

You should assign a valid email address to your email variable.

js
assert.match(email, /^[a-zA-Z0-9._%-]+\+?[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/);

maskEmail("[email protected]") should return "a*******[email protected]".

js
assert.strictEqual(maskEmail("[email protected]"),"a*******[email protected]");

maskEmail("[email protected]") should return "f**********[email protected]".

js
assert.strictEqual(maskEmail("[email protected]"), "f**********[email protected]");

maskEmail("[email protected]") should return "i**[email protected]".

js
assert.strictEqual(maskEmail("[email protected]"), "i**[email protected]");

maskEmail("[email protected]") should return "u**[email protected]".

js
assert.strictEqual(maskEmail("[email protected]"), "u**[email protected]");

Your maskEmail should produce the correct result.

js
assert.strictEqual(maskEmail("[email protected]"), "j***********[email protected]");
assert.strictEqual(maskEmail("[email protected]"), "j******[email protected]");
assert.strictEqual(maskEmail("[email protected]"), "j********[email protected]");
assert.strictEqual(maskEmail("[email protected]"), "m************[email protected]");
assert.strictEqual(maskEmail("[email protected]"), "m*********[email protected]");

You should log the output of calling maskEmail with email as argument.

js
assert.match(__helpers.removeJSComments(code), /console\.log\(maskEmail\(email\)\)/);

--seed--

--seed-contents--

js

--solutions--

js
function maskEmail(email) {
  const atIndex = email.indexOf("@");
  const userName = email.slice(0, atIndex);
  const domain = email.slice(atIndex);

  const maskedName =
    userName[0] + "*".repeat(userName.length - 2) + userName[userName.length - 1] + domain;

  return maskedName;
}

const email = "[email protected]";
console.log(maskEmail(email));