Tell us what’s happening:
I have done this in a very, very round about way.
I am getting the correct results but its still saying its a fail.
I assume its the method I used to make it work.
This probably being the most round about way to get to the answers. Any help would be appreciated.
Your code so far
let email = "Toothpick164@example.com";
function maskEmail(email) {
let indexNum = email.indexOf("@");
let domainOnly = email.slice(indexNum);
let firstLetter = email.slice(0, 1);
let lastIndex = indexNum - 1;
let lastLetter = email.slice(lastIndex, indexNum);
let toMask = email.slice(1, indexNum);
let masked = toMask.replaceAll(/[a-zA-Z0-9._-]/g, "*");
let maskedEmail = firstLetter + masked + lastLetter + domainOnly;
console.log(maskedEmail);
return;
console.log("Email: " + email);
console.log("Index number: " + indexNum);
console.log("Domain only: " + domainOnly);
console.log("First Letter: " + firstLetter);
console.log("Last Letter: " + lastIndex);
console.log("Last Letter: " + lastLetter);
console.log("To mask: " + toMask);
console.log("masked text: " + masked);
console.log("Full Mask: " + maskedEmail);
return;
}
maskEmail("apple.pie@example.com");
maskEmail("freecodecamp@example.com");
maskEmail("info@test.dev");
maskEmail("user@domain.org");
maskEmail(email);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Challenge Information:
Build an Email Masker - Build an Email Masker
a2937
January 21, 2026, 12:00am
2
Have you compared the length of the masked email with the actual email? That could easily be something that causes an error.
Also what is your function returning?
ILM
January 21, 2026, 8:14am
3
what is your function returning?
toothpick164:
return;
your return is just this, what does this make the output of the function?
I did not notice I was masking an extra letter thank you.
I cant get the return to do what I want though.
I’ve tried;
return email;
return maskEmail;
return console.log(email);
return console.log(`“${firstLetter}${masked}${lastLetter}${domainOnly}”`);
Not sure why I’m getting the result but its not passing.
Just having return; will end the function but after trying so many alternatives that had no output into the log I left it blank. I replied to another with some of the options I have tried
This is what I currently have that is producing the asked for results.
let email = "Toothpick164@example.com";
function maskEmail(email) {
let indexNum = email.indexOf("@");
let domainOnly = email.slice(indexNum);
let firstLetter = email.slice(0, 1);
let lastIndex = indexNum - 1;
let lastLetter = email.slice(lastIndex, indexNum);
let toMask = email.slice(1, indexNum -1);
let masked = toMask.replaceAll(/[a-zA-Z0-9._-]/g, "*");
email = `${firstLetter}${masked}${lastLetter}${domainOnly}`;
return email;
}
console.log(maskEmail(email));
console.log(maskEmail("apple.pie@example.com"));
console.log(maskEmail("freecodecamp@example.com"));
console.log(maskEmail("info@test.dev"));
console.log(maskEmail("user@domain.org"));
console.log(maskEmail(email));
ILM
January 21, 2026, 11:27am
7
you are missing some valid characters in the email address
can you do this be more generic? or consider, do you really need to repleace, or just a string of * of the right length?
so I changed the code a bit. I removed toMask and masked. changed the email = into concatenation instead of ` and changed the masked section into .repeat using indexNum. This produced the same result and passed. What was the main reason my initial code failed? Even though it was producing the correct answer it was null. Is this due to the replace solely or another issue that I am missing? If its something I don’t understand now it will become an issue in future code.
Thank you for the help so far too
ILM
January 22, 2026, 9:44am
9
the main reason is that + is a valid character in emails, and your code was not replacing it, like I said here