15. Mask Email on Account Recovery
On "Forgot password?", after a user types their username, the page says "We sent a code to j***e@gmail.com". The address must be recognisable to its owner but useless to someone probing accounts.
The mask uses a fixed number of stars so it does not reveal the length of the username, and it hides plus-address tags like "+shop", which can reveal where the user signed up.
Your task:
Write maskEmail(email) and return the masked address, or "" when the input is not a valid email.
Rules: - Trim surrounding whitespace and lowercase the whole address. - A valid email has exactly one "@", with a non-empty part before and after it. - Remove a "+tag" from the local part (everything from the first "+" up to "@") before masking. - Local part of 3 or more characters: first character + "***" + last character. 2 characters: first character + "*". 1 character: unchanged. - The domain is shown unchanged.
Examples
Example 1
- Input
email = " John.Doe+Shop@Gmail.com "
- Output
"j***e@gmail.com"
- Explanation
- Trimmed and lowercased, "+shop" removed, then "john.doe" is masked.
Example 2
- Input
email = "a@b.com"
- Output
"a@b.com"
- Explanation
- A one-character username stays as it is.
Solution
Write maskEmail. Tab indents (Esc then Tab leaves the editor), Ctrl/⌘ + Enter runs. Each run has a 3-second limit. Your code is saved in this browser.
Test Results
Click "Run Code" to test your solution.