Build an Email Masker - Build an Email Masker

Tell us what’s happening:

I cannot figure out how to use replace() and slice() to mask the username part. The code on lines 1-4 and after line 6 works fine.

Your code so far

let email = "robbass@gmail.com";
const maskEmail = (email) => {
  const slicedEmail = email.slice(0, 7);
  console.log(slicedEmail);
  const maskedEmail = slicedEmail.replace("obbas", "*****");
  console.log(maskedEmail);
  const domain = email.slice(7);
  console.log(domain);
  const newEmail = maskedEmail + domain;
  return newEmail;
}
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/145.0.0.0 Safari/537.36

Challenge Information:

Build an Email Masker - Build an Email Masker

Here, you hard-coded informations in your code. Learn here more about hard-coding. You have to think that it has to work not only for this specific email, but for all emails.

Hmm, I see. If hardcoding does not work, how do I combine the replace() and slice() methods using the first and last indexes to mask any email?

For example, the code I always think of is

const maskedEmail = slicedEmail.replace(slicedEmail.slice(1, -1), “*”), but I don’t understand how to use the repeat() for the asterisks without hardcoding.

You’re on the good path, but you need to review a few points in your code.

  1. Here, slicedEmail is hardcoded. It will always take from the 0 index character to the 6 index character , no matter the email’s length. Hint : Think about the fact that all email adresses have a “@” character. You could use the indexOf() function too.
  2. Your idea was really good ! const maskedEmail = slicedEmail.replace(slicedEmail.slice(1, -1), “*”) is an excellent beginning. Now all you need to do is to is to implement the replace() syntax. Use the lesson to help you. If you can’t understand it, we will help you further on that. Hint : Think about the length property of your string.

Those are excellent suggestions! Let me catch up on that in a couple of hours.

1 Like

After analyzing your suggestions above, I was able to figure out the issue! First, I needed to define the replacementCharacter outside of my function in order to use the repeat() method for the asterisks. Secondly, I had to define the slicedEmailLength to determine the asterisk replacement amount. Thirdly, I needed to use the indexOf() method for the @ symbol in the domain operation.

Essentially, I had to do a lot of defining in order to use the methods.

Thank you so much for your help!

Well, if you got it, congrats ! :partying_face: It was a tough step, but you managed trough it ! Don’t give up, and I wish you the best of luck during your learning ! Enjoy coding :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.