Build an Email Masker - Build an Email Masker

Tell us what’s happening:

I got solve the problem already, but I’d like to know about better ways to perform it!!

Every opinion would be useful for me!!

Your code so far

function maskEmail(email){
  const dot = email.indexOf('@')
  const userName = email.slice(0, dot)

  const domain = email.slice(dot)

  const userInfo = userName.slice(1, -1)
  let char = '*'

  const hiddenInfo = userInfo.replace(userInfo, char.repeat(userInfo.length))

  var firstChar = email[0]
  var lastChar = email[userName.length - 1]

  const newEmail = `${firstChar}${hiddenInfo}${lastChar}${domain}`
  return newEmail
  
}

var email = 'someone@example.com'

console.log(maskEmail(email))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0

Challenge Information:

Build an Email Masker - Build an Email Masker

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-email-masker/66b205e6eacba4c4e54ea434.md at main · freeCodeCamp/freeCodeCamp · GitHub

In the future, do NOT create duplicate topics. This is against forum rules.

Thank you.

Hi @edgshift

Add comments explaining what each part of the code does.

You’ve done a good job of obtaining the first and last characters of the user name.

For the number of asterisks don’t you need just the length of the user name minus 2?

Happy coding

I got you, but I just did it to get feedback. In the first post, I could accomplish

I didn’t think about, but for me was easier to get the length through 1 and -1

Don’t worry about optimising code for this part of your journey.

Do whatever feels comfortable for you. Learn the fundamentals, review it, then move on.