Learn Form Validation by Building a Calorie Counter - Step 24

Tell us what’s happening:

Some requests have been made in the tutor prompt, and this is how I perceive the solution to be, whats wrong with it

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

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

}

// User Editable Region

Your browser information:

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

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 24

Use your regex to replace all instances of + , - , and a space in str with an empty string. Return this value.

You are not using the str parameter but a string that contains literally the characters "str", you are also not returning the value.

But this seems to be how the example handled it

Hi there!

Take the example seriously as an example.

So the example does not help solve the question?

the example is targeting the literal string "hello", you need to target the str parameter. Also you need to return the value

the example shows how to use the replace method, it can’t be used exactly as is

also the example writes the regex directly as argument of replace, you need to use the regex variable that holds the regex you were writing previously

Use the regex variable?

So you’re saying this?:

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

this regex variable, you need to use it as first argument of replace

you also still need to return

Would that be this:

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

}

no, it’s a regular expression: where do you have a regular expression in the example?

The example is
"hello".replace(/l/g, "1");
in your code you need to have the string you need to make changes to in place of "hello", the regular expression that captures the things you need to change in place of /l/g and the string with the new thing to put in the string in place of "1"

The previous exercise uses the variable regex

the previous step makes you create it, not use it, now you need to use it

You have stored a regular expression pattern /[+-\s]/g in the regex variable in previous step.
Now you need a return statement that returns the parameter str with replace method using dot notation and use the regex variable and an empty string "" separated by a comma within the .replace() method. Your returning statement should be one line of code.

Wouldnt it be the parameter str

So you’re insinuating this:

function cleanInputString(str) {
  const regrex = /[+-\s]/g;
  str.replace(/+-/g);
  return(str);

}

you need to return this

also, you need to put two arguments in the parentehses of replace, one is the regex variable, then you need a second one as described in the instructions

what would the parameter str be?

Isn’t str the parameter

str is the parameter, yes, but what was the question related to?