Build an Email Masker - Build an Email Masker

Tell us what’s happening:

The code returns the correct output but does not pass. I think the error is in the for loop function but I cannot locate it. The code has two functions: an initial function that splits the email into the domain and the handle and then sends the handle to a second function that has a for loop/nested if statement for masking the handle. It then returns back the masked handle to the original function to display the output via concatenation.

I have counted the masked output characters for the examples and it all adds up so I am stumped as to where the mistake it. I am assuming it’s somewhere in the for loop but I cannot seem to figure it out. The for loop function is as follows:

function returnNewHandle(handle) {
  let handleArray = handle.split('');
  let newHandle = ' ';
  for (let i = 0; i < handleArray.length; i++) {
    if (i === 0 || i === handleArray.length - 1) {
      newHandle += handleArray[i];
    } else {
      newHandle += "*";
    }
  }
  return newHandle;
}

Thanks for the help

Your code so far

let email = "apple.pie@example.com";

function maskEmail(email) {
  const symbol= "@";
  const symbolIndex = email.indexOf(symbol);
  const domain = email.slice(symbolIndex);
  const handle = email.slice(0, symbolIndex);

  let maskedHandle = returnNewHandle(handle);
  return maskedHandle + domain;
}

function returnNewHandle(handle) {
  let handleArray = handle.split('');
  let newHandle = ' ';
  for (let i = 0; i < handleArray.length; i++) {
    if (i === 0 || i === handleArray.length - 1) {
      newHandle += handleArray[i];
    } else {
      newHandle += "*";
    }
  }
  return newHandle;
}

console.log(maskEmail(email));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0

Challenge Information:

Build an Email Masker - Build an Email Masker

Welcome to the forum @jmbehr12

You have an extra space at the beginning of the string.

 a*******e@example.com
^

Happy coding

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').