Learn Form Validation by Building a Calorie Counter - Step 28

No sure exactly what they are asking. Here is what I have:

function cleanInputString(str) {
  const regex = /[+-\s]/g;
  str.replace(regex, "")
  
}

it keeps telling me " Your clearInputString function should directly return the results of your replace method"

Need some help, not sure what to do. Thanks!

1 Like

so where is the return statement?

1 Like

I added a return, but it is incorrect. what am I missing?

function cleanInputString(str) {
const regex = /[±\s]/g;
str.replace(regex, “”)
return str;

1 Like

you are returning str, you need to return the output of replace, remember that strings are immutable, so str is the same as what was inputted to the function

3 Likes

I tried using replace instead of str, but it didn’t work. When you refer to returning the output of replace, what are you referring to?

function cleanInputString(str) {
const regex = /[±\s]/g;
str.replace(regex, “”)
return replace;

1 Like

you have written str.replace(…) but how are you saving what is returned there?

1 Like

No sure. I’m really stumped.

you have written it on its own line to slowly build the expression, but until now you have done nothing with the value returned by replace, you need to return it. You return something in a function by writing return at the beginning of a line and to its right what you want to return

2 Likes

right, I hear you. could you give me an example? much easier to visually see what you are trying to explain. I have been working on this problem all afternoon and have researched different websites to help solve this challenge. I’ve seen how return is used, but there must be something i’m missing , which I can’t grasp through written text.

if you want to return 3 from a function, you write return 3 as its last line, if you want to return the value from a variable called output, you write return output as last line of the function, you want to return the value from replace

2 Likes

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

You need just to RETURN it!!

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

Great discussion! Took me awhile, but the key is in lesson ‘learn-basic-javascript-by-building-a-role-playing-game/step-147’ from the prior learning project.

You should return the function directly.

1 Like

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