Build an Email Masker - Build an Email Masker

Tell us what’s happening:

my first 2 are correct yet my other 2 aren’t showing asterix correctly

Your code so far

const maskEmail = email => {
  const str1 = email.slice(0, -(email.length - 1));
  const str3 = email.slice(-13);
  const str2 = email.slice(1, -13);
  let replacementString = "*";
  const repeatReplacementString = replacementString.repeat(str2.length);
  const replacedPart = str2.replace(str2, repeatReplacementString);
  return str1 + replacedPart + str3;
}

let email = "apple.pie@example.com";
console.log(maskEmail(email));
email = "freecodecamp@example.com";
console.log(maskEmail(email));
email = "info@test.dev";
console.log(maskEmail(email));
email = "user@domain.org";
console.log(maskEmail(email));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Build an Email Masker - Build an Email Masker
https://www.freecodecamp.org/learn/full-stack-developer/lab-email-masker/build-an-email-masker

why do you think some works and some don’t?

Only my 7, 8 and 9 don’t pass unless im wrong and more of my code is incorrect and it’s
passing due to certain requirements being hit through other code?

and what’s the difference between the email addresses used in the passing tests and in the not passing tests?

I have restarted my code and managed to get it to the point where only 9 isn’t passing. I’ll create a new topic.

please don’t create a new topic, only one topic per challenge is allowed

1 Like

Tell us what’s happening:

Test 9 isn’t passing. Here is what appears on my console :

ae@example.com
f
***p@example.com
io@test.dev
u
r@domain.org

This looks right to me?

Your code so far

let email = "apple.pie@example.com";

const maskEmail = email => {
  const atIndex = email.indexOf("@");
  const domain = email.slice(atIndex);
  const username = email.slice(0, atIndex);

  const firstChar = username[0];
  const lastChar = username[username.length - 1];
  const middle = username
    .slice(1, -1)
    .replace(/[a-zA-Z.]/g, "*");
  return firstChar + middle + lastChar + domain;

}

console.log(maskEmail(email));
email = "freecodecamp@example.com";
console.log(maskEmail(email));
email = "info@test.dev";
console.log(maskEmail(email));
email = "user@domain.org";
console.log(maskEmail(email));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Build an Email Masker - Build an Email Masker
https://www.freecodecamp.org/learn/full-stack-developer/lab-email-masker/build-an-email-masker

there are this amount of asterix in each line :

  • 7
  • 10
  • 2
  • 2

what happens if there is a mail like email+newsletter@gmail.com, does your app manage to mask it all?

It doesn’t mask the “+”

that seems something you need to fix

what about if there are numbers?

I have passed the code. Just so I understand this more, you have to put in each character that you are wanting to mask when using replace() and you would have to input whatever special characters that are available to use in the input field?

there are various ways in which this can be some, there are some of the ways that don’t include specifying the characters to mask, your way of solving it requires to specify the characters