Build an Email Masker - Build an Email Masker

Tell us what’s happening:

I can’t get the right amount of * to repeat, I’ve been stuck on this for a while, but my brain is apparently just not working, so any help would be appreciated!

Your code so far

const maskEmail = (email) => {
  const indexOfAt = email.indexOf('@'); // gets index of @
 const domain = email.slice(0, indexOfAt);
 const domName = email.slice(indexOfAt);
 const firstChar = domain.slice(0, 1);
 const lastChar = domain.slice(domain.length -1, domain.length); // extracts last character
 const midChar = domain.slice(1, domain.length, -1);
 const lengthOfMidChar = midChar.length;
 const maskedDomain = "*".repeat(lengthOfMidChar);
 return firstChar + maskedDomain + lastChar + domName;
 
}

let email = ("example@email.com");

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"));


Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Build an Email Masker - Build an Email Masker

How many stars are appearing? Is it more or less than you need?

Is it random or is there a consistent pattern?

Which variable are you using to determine the number of asterisks? Have you logged it to see if it’s what you expect?

verify each variable, add console.log, check the value of each one

or use a tool like Python Tutor code visualizer: Visualize code in Python, JavaScript, C, C++, and Java that will also allow you to see the value of each thing

there’s just an extra star when there doesn’t need to be, it’s for all of the results, i just don’t know how to figure it out.

try debugging, make sure you know what the value of each variable is, do not assume that it has the correct value. If you do that you will figure it out

There’s one extra star every time? It may not be “the best” solution but it sounds like you can add a - 1 somewhere?

oh okay, do you have any idea where might be the best, i’ve been playing around with the numbers, but nothing has worked so far.

it seems to be a problem with the middle characters, the length keeps coming back an extra letter than it should, but i genuinely don’t know how to fix it.

it seems you are not looking at the values of your variables as you should

so please add console.log to check the value of all variables or use Python Tutor code visualizer: Visualize code in Python, JavaScript, C, C++, and Java

once you see the value that is wrong you are going to quickly solve it

let email = ("example@email.com");
console.log(maskEmail(email));

in this case, whats the value of indexOfAt? is it the value you expect?
what’s the value of domain? is the value you expect?
what’s the value of domName ? is the value you expect?
what’s the value of firstChar ? is the value you expect?
what’s the value of lastChar ? is the value you expect?
what’s the value of midChar ? is the value you expect?
what’s the value of lengthOfMidChar ? is the value you expect?
what’s the value of maskedDomain ? is the value you expect?

Which line of code calculates the middle letters?

Which line of code generates the stars?

Use console.log() within your function as suggested by ILM to investigate.