Learn Form Validation by Building a Calorie Counter - Step 28

Tell us what’s happening:

Step 28

Strings have a .replace() method which allows you to replace characters in the string with another string. .replace takes two arguments. The first is the character sequence to replace – this can either be a string or a regex pattern. The second is the string to replace that sequence with. For example, this would replace all instances of l with 1:

"hello".replace(/l/g, "1");

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

Your code so far

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

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

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

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 28

please help!!!please help !!!

Hello!
The + - is not correct here.

It appears to be a + with an underline, or underscore.


[quote="mian22, post:1, topic:663009"]
Use your `regex` to replace all instances of `+`, `-`, and a space in `str` with an empty string.
[/quote]

The above, from your post, shows how to enter the code correctly.

Wishing you good progress.

it says “You should pass "" as the second argument to replace .”
[± it is written like this: ±
please help me?

this programme writing wrong…+"-

So, after struggling for a while you actually have to directly return that value… So use return (.replace()) - this will directly return the value… but you have to figure it out for yourself the code… and i don´t think you are supposed to change the const you have, but to use the value in the regex to replace it with an empty string, “”. Maybe Reset the step and start with a clean code… i don’t think i can say more, or show you without having in mind the struggle… have a good one!

Reset your code.

  1. The regex is already written on the first line of the function. Do not change that.

  2. On the line below the regex, call .replace() on the str parameter.

  3. Pass .replace() the regex as the first argument and the replacement value "" as the second argument.

  4. The value returned from .replace() should be returned out of the function.


function removeWildcards(str) {
  const regex = /\*/g;
  return str.replace(regex, "")
}

console.log(removeWildcards("abc*de*f")); // abcdef
1 Like