Helpful tips to learn functions
Posted by Chrxme11@reddit | learnprogramming | View on Reddit | 39 comments
Hello everyone
I’m currently a CS graduate and I’ve been trying to learn JavaScript I’ve been watching YouTube videos (which I feel like weren’t good for me)
I came across freeCodeCamp and so far so good I was able to breeze by the first two sections.
Now I’m currently learning about functions and how they work and I’ll hit a bump trying to build an email masker.
I don’t wanna rely on AI because i actually wanna learn. I tried having AI break it down to me and I wasn’t grasping it.
But I was tasked with
-
Create a function named maskEmail that takes email as an argument.
-
Inside the function, you should mask the email and append the domain name to it. Remember that you can use methods like slice, repeat, indexOf or even replace to help you.
-
Outside the function, declare a variable named email to store the email address you want to mask.
-
Call the maskEmail function with the email variable and output the result to the console.
-
maskEmail("apple.pie@example.com") should return "a*******e@example.com".
-
maskEmail("freecodecamp@example.com") should return "f**********p@example.com"
-
maskEmail("info@test.dev") should return "i**o@test.dev"
-
maskEmail("user@domain.org") should return "u**r@domain.org".
So far I was only able to get pass step one I tried rereading about functions but I still didn’t understand I tried watching YouTube videos but I could understand it neither.
I started to think maybe it was brain issue with me of how I decipher what’s being said to me in English to code and I was just wondering if anybody can give me tips or advice how to become a better coder I’m really eager to pursue a career in SWE
Chrxme11@reddit (OP)
function maskEmail(email) {
const atIndex = email.indexOf("@"); const username = email.slice(0, atIndex); const domain = email.slice(atIndex); const firstLetter = username.slice(0, 1); const lastLetter = username.slice(-1); const middle = "*".repeat(username.length - 2); return firstLetter + middle + lastLetter + domain; }
const email = ("myEmail@email.com"); console.log(maskEmail(email));
marrsd@reddit
Pro tip: you can format code in Reddit
const email = ("myEmail@email.com"); console.log(maskEmail(email));
Chrxme11@reddit (OP)
I was typing on my phone not my actual pc sorry
marrsd@reddit
It'll still work on your phone. Just prefix each line with 4 spaces. Makes it much easier for us to read the code
Chrxme11@reddit (OP)
That felt the hardest code I ever wrote in my life seriously I was pulling my hair out literally stressing I was stuck on this for 4 days I was ready to range so bad 🤦🏾♂️😂 I’m gonna keep practicing though
grantrules@reddit
Good job! You just have to learn to reason through problems. I didn't give you any answers, I just broke it down into small problems and asked questions. You were able to answer the questions with a little prodding, you just need to learn to break things down and ask yourself those questions. No need to guess in programming, you can either look it up or try it.
KandevDev@reddit
the trick that helped me: stop reading about functions and start writing them, but with a constraint. for one week, refactor every for-loop you write into a named function. even when it is a 2-line loop. you will start to feel the "this loop has a single responsibility" intuition that is the underlying skill. functions are not a concept to learn, they are a habit your code falls into once you have practiced the abstraction enough times.
Chrxme11@reddit (OP)
Thank you and right now I’m currently going through the freeCodeCamp journey of learning JavaScript so I know arrays cause when I was in community college I learned arrays but for JavaScript I haven’t learned it so I’m going to use it until I learn it I feel like I understand how to create and setup the function but writing inside that function was the hard part for me I was like well what do I write
Chrxme11@reddit (OP)
I finally got it guys you guys helped me out so much
Chrxme11@reddit (OP)
Thank you everyone who helped out I really appreciate it man !
expiyr@reddit
Ask yourself 1.) How do I get the username (and the non-username part) 2.) How do I get the specific part I will need to mask in the username 3.) How do I return the masked username together with the non-username part
grantrules@reddit
So it doesn't seem like your problem is with functions, it seems like your issue is with "you should mask the email and append the domain name to it. Remember that you can use methods like slice, repeat, indexOf or even replace to help you."
Try just breaking down what you're trying to do into smaller steps..
You need to get the text before the @ and you need to get the text after the @
How would you do that? (Hint: The instructions give you the tools you need)
Chrxme11@reddit (OP)
Yeah I feel like that’s throwing me off cause so far I have
function maskEmail(email) {
}
But now moving to step 2 I’m confused like huh how do I even do that I know I need to write something inside the function but what is it I feel so bad at this 🤦🏾♂️😂
grantrules@reddit
It's literally telling you Remember that you can use methods like slice, repeat, indexOf or even replace to help you.
So maybe look those up. You need to get the text before the @. Can you figure out any way to use those methods listed to do that?
Chrxme11@reddit (OP)
So then
const username = email.indexOf(“@“); ?
grantrules@reddit
You're just guessing. Stop. Research. Make informed decisions.
What does
indexOfdo?Chrxme11@reddit (OP)
IndexOf search’s for the strings I think of it as your find command when your reading text and you want to look for a specific word
grantrules@reddit
What does it return?
Chrxme11@reddit (OP)
It’s returns the word you put inside the indexOf()
When you console.log it
Chrxme11@reddit (OP)
So if you create a variable called
Const text = “hello world this is some random text”
Let result = text.indexOf(“world”);
It’s returns world
zeekar@reddit
No, it doesn't. Are you even looking?
Last I checked the number 6 is not the string "world". Look. Read. TRY.
Chrxme11@reddit (OP)
const atIndex = email.indexOf("@"); const username = email.slice(0, atIndex); const domain = email.slice(atIndex); const firstLetter = username[0]; const lastLetter = username[username.length - 1]; const middle = "*".repeat(username.length - 2);
grantrules@reddit
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
Can you read that please. Then tell me again what indexOf does.
Chrxme11@reddit (OP)
The indexOf() method in JavaScript allows you to search for a substring within a string.
grantrules@reddit
And what does it return?
Chrxme11@reddit (OP)
It’s returns the string so like I was saying before
let sentence = ("JavaScript is awesome"); let position = sentence.indexOf("awesome");
console.log(position);
Its returns 14 in the console
grantrules@reddit
Well, I wouldn't call that returning the string, it's returning the index.
But with this code, you can determine where the @ is in an email address, right?
Can you tell me which of the other suggested methods might be useful in getting
usernamefromusername@email.com? Remember, we know where the @ is, so we know how how long the username is.Chrxme11@reddit (OP)
Ohhh so we use the indexOf to figure out what number the @ stops at to then use slice to ? Did I get that correct ?
grantrules@reddit
You tell me. Does it work?
grantrules@reddit
So if I do blah.indexOf("@") it returns @? What is the use of that?
johnpeters42@reddit
Okay, so email.indexOf("@") is part of what you need. That's the position where the @ is.
Now that you've got that, how do you get all the text before that position? How do you get all the text after that position?
Chrxme11@reddit (OP)
function maskEmail(email) {
const atIndex = email.indexOf("@"); const username = email.slice(0, atIndex);
}
const email = ("myEmail@email.com");
grantrules@reddit
So what next?
Chrxme11@reddit (OP)
I’m so slow guys I had the first step the whole time I never applied it
const username = email.slice(0, atIndex);
idk_01@reddit
check out Substring() in javascript.
also, check out Split().
Chrxme11@reddit (OP)
Will do
Chrxme11@reddit (OP)
You can use slice then
johnpeters42@reddit
First, can you make the function just make some change to the string, and get through steps 1 through 4 that way? Like just "add a * character at the end".
Once that's done, change the function to make the actual change that you want. How would you do that by hand? Which part of the string is the domain name? How do you know that? What do you need to do to the other part? Then work on translating that English description into code that follows the same rules.
Chrxme11@reddit (OP)
I tried and I just keep hitting a blank wall I feel like I asked AI to give me a push but not the answer but I was so confused trying to grasp it maybe my learning style isn’t good for JS ? I came to realize