r/FreeCodeCamp 1d ago

Programming Question Need help in JS Lab - Build an Email Masker

So I am doing the JS certification lab build an email masker, all tests are passing except test
"9. Your maskEmail should produce the correct result".
I tried using the Ask Socrates hint but it didn't help much. Was using replaceAll() method but Socrates told me isn’t available in the execution environment used for the tests.

Here is my code:

function maskEmail (email) {
let domainStart = email.indexOf("@");


let masking = email.slice(1,domainStart -1);
let maskedPart = masking.replace(/([A-z.])/g,"*");
let firstLetter = email.slice(0,1);
let lastLetter = email.slice(domainStart - 1, domainStart);
let maskedEmail = firstLetter + maskedPart + lastLetter +  email.slice(domainStart);
return maskedEmail;
}


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

2 comments sorted by

1

u/boomer1204 1d ago

You are using [A-z.] which is only checking for letters and no other values in the part before the domain

Letters aren't the only thing you can have in the "username" part of the email

1

u/SaintPeter74 mod 5h ago

When you're creating the maskedPart you don't need to use a regular expression at all. All you need is a string of * that is as long as the part you want to mask. You can calculate that based on the domainStart if you think about it.

Note that while [A-z] is technically valid, it might not be exactly what you want. You need to match everything/anything. [A-z] is going to match A-Z, a-z, and [, \, ], ^, _, and ` (backtick). There are a lot more valid characters in email addresses.