Help with String Slice - total beginner

Hi,

I dont understand why my code is being sliced at character 0 rather than character 5.
When if I console.log position I get the character I want.
Please help

I know this perhaps isnt the most concise way to do this but I’m trying to write everything out step by step so I can check as I go.

function secure (email){

  let whereAt = email.search("@"); 
  let position = email.charAt(5);
  let substring = email.slice(position, whereAt);
  
  let ans = email.replace(substring, "..........");
  return ans;

 }
console.log (secure("REDACTED"));

Thank you!

The String.slice() method takes a number representing the index in the string where you want to begin the slice for the first argument. In your code you are setting position to the character at the 5th index in the email string, which is not a number but rather of string of one character. Slice is automatically converting that character to a number and in your test case the character at the 5th index is ‘e’ which gets converted to the number 0 by slice and thus that is why the slice is beginning at 0.

Thank you so much!
Basically i was over complicating it and could have just put the number 5.

Thank you

I have removed your personal email address because of google indexing.


I’ve edited your post for readability. 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 (’).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.