what is the function within the function’s body
Did you find out how .replace()
works? Did the article help?
a little bit,
this is the code I am now working with:
const removeSpecialChars = () => {
removeSpecialChars.replace(/[^a-zA-Z0-9]/g, "");
return removeSpecialChars();
}
Great work so far.
You can see that your replace method replaces the special character from removeSpecialChars
. Think of it as if .replace
is doing that to any string. It can be a variable called Username for example, like this:
Username.replace(/[^a-zA-Z0-9]/g, "");
In this case, any special characters from Username will be replaced. But where did the variable (Username) come from?
An arrow function accepts parameters that could be used inside the fuction. the parameter is added between the brackets in const removeSpecialChars = ()
.
I’ve given examples of it previously like:
const myFuncton= (message) => {
console.log(message);
};
The above code takes a variable called message and uses it inside console.log In your case think of it as taking the variable message and removing the special characters from it.
I hope it’s not confusing.
Good luck!
I guess what the task at hand now would be to find the replacement of Username
…
Keep going. You’ll figure it out.
There doesn’t seem to be any other item to be used as such other than removeSpecialChars
…
How does your code look so far?
You should just add a parameter to your arrow function and then use that with .replace()
inside your function.
What is the parameter?
How the code is looking now…
const removeSpecialChars = () => {
.replace(/[^a-zA-Z0-9]/g, "");
return removeSpecialChars();
}
A parameter is a variable that is writen between the paranthesis. It recieves input value that the function can use.
for example:
const removeSpecialChars = (string) => {
return string;
}
Here string is a parameter. It can contain any text based on the input and whatever it is, the function uses it.
Right now your function doesnt have a parameter. It should have one. According to the instructions, Your function should receieve a variable and remove that variable’s special characters. You add that variable as a parameter.
Hope it’s clear.
I dont think the question states what the parameter is as well, it just says “a string”…
That’s the thing. You just give it a random name. It’s a variable. You can call it string or whatever you like
So this should work?:
const removeSpecialChars = (astring) => {
astring.replace(/[^a-zA-Z0-9]/g, "");
return astring();
}
Try it and let’s hope it works!
Also astring is a variable not a function. so remove the paranthesis at the end.
The prompt says:
Your removeSpecialChars should remove single quotes.
Okay that means now the problem is with your regex here:
/[^a-zA-Z0-9]/g
I suggest you find out what this regex currently removes, and then try to see how you can remove single quotes as well.
This link may help.
But the question asks only to remove special characters…
Yes, but then it also tells u to remove single quotes. Like earlier in your error. So you have to look for ways to do that.