Build an Email Masker - Unintentionally adding extra characters

Tell us what’s happening:

So I passed the lab with this code however I’d like help understanding why the code initially adds an extra two asterisks to the email censorship. I want to know since there’s definitely a better way of doing this rather than just cutting out whatever extra pieces get added in somehow.

Your code so far

function maskEmail(email) {

let splitPart = email.indexOf("@"); 					
let sliced = email.slice(1, (splitPart -1)) 
let hide = "*";

let hiddenmail = email.replace(sliced, hide.repeat(splitPart -2)); 	
                                                                            
return hiddenmail

}

let email = ("apple.pie@example.com")

console.log(maskEmail(email))
maskEmail("freecodecamp@example.com");
maskEmail("info@test.dev");
maskEmail("user@domain.org")

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36

Challenge Information:

Build an Email Masker - Build an Email Masker

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-email-masker/66b205e6eacba4c4e54ea434.md at main · freeCodeCamp/freeCodeCamp · GitHub

this behaviour is not present in your code, please post the code you are asking about

Sorry, here was my initial code that added the extra asterisks. The only difference was instead of having a -2, I initially only had a -1 after splitPart

I see one extra asterisk in this, and it depends on splitPart -1

remember that repeat take the number of characters you want repeated, while splitPart is an index

Ah right apologies, it must’ve been just 1 extra one it added.

So I assume I’m adding an extra character somewhere that the repeat function picks up on?

the extra character is the difference between splitPart -1 and splitPart - 2

but to understand why you need that subtraction you need to understand that splitPart is an index, not a length

Okay gotcha, I kinda understand now. I’ll go back and revise it myself to make it fully click. Thank you for your help!