Build an Email Masker

For some reason I can’t get the site to accept my answer. I’ve ran some tests within the code to make sure my output matches what the correct answer are, and they all come back as true. Below is my code and the results in the console. Any suggestions would be appreciated.

function maskEmail(emailMask) {
  let hidden = "*";
  let maskedEmail = email[0] + hidden.repeat(email.indexOf("@") - 2) + email[email.indexOf("@") - 1] + email.slice(email.indexOf("@"));

  // testing redacted email matches answer
  if (maskedEmail === "a*******e@example.com") {
    console.log(true);
  } else if (maskedEmail === "f**********p@example.com") {
    console.log(true);
  } else if (maskedEmail === "i**o@test.dev") {
    console.log(true);
  } else if (maskedEmail === "u**r@domain.org") {
    console.log(true);
  }

  return maskedEmail;
}

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

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

email = "info@test.dev";
console.log(maskEmail(email));

email = "user@domain.org";
console.log(maskEmail(email));

Please post your code instead of a picture of your code.

Also, please post a link to the lab.

Thanks

Thank you for editing to add your code. Please add a link to the lab/project.

You need to use the function argument. Using global variables breaks your function.

Thank you. I forgot to go back and change those when i consolidated my code.

1 Like