Learn Form Validation by Building a Calorie Counter - Step 22

Tell us what’s happening:

Can someone please help me with my code! I have tried several method but, I am still stuck.

Your help will highly be appreciated

the instruction says: " Within your loop, you need to check if the character in strArray at index i is not a +, -, or a space. If it is not, push it to the cleanStrArray.

You will need to check if the array ["+", "-", " "] does not include the current character. You can use a combination of the includes() method and the ! operator to do this."

Your code so far

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.

function cleanInputString(str) {
  const strArray = str.split('');
  const cleanStrArray = [];

  for (let i = 0; i < strArray.length; i++) {
    if (cleanStrArray = !["+", "-", " "].includes()){
      
    }
  }
}

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 22

Hi @hiekamara1

At the moment the i variable is not accessing the correct array.

  1. Carefully read the instructions on how to use the .includes method
    Then make sure to use the array mentioned in the instructions.
  2. Add the code for pushing the value to the other array.

Happy coding

1 Like

Hello there!

It seems like you misunderstood the way .includes() works, as well as the NOT (!) operator.
As said in the exercise, the .includes() method returns True if the element in between parenthesis is part of the Array of specified elements ["+", "-", " "]. To use it, you need 2 objects:

  • Your array of elements
  • The object you want to check (here it is the character with index i in the strArray)

Now let’s make the link with the NOT operator: since .includes() returns True or False, you don’t need to use an equal sign, the correct syntax in your case will be:

if (!["+", "-", " "].includes()) { // In between the parenthesis, put whatever you need to check!
 // Your action (push the item in the cleanStrArray as specified in the task)
}

This can be translated in plain english as “If not this array ["+", "-", " "] include whatever object then do this”.

I tried my best to explain as clearly as possible, I really hope it is helpful for you and do not hesitate to ask more questions if necessary.

Good luck!

1 Like

Hey Losarig
Thanks man, but I am still stuck here !

 for (let i = 0; i < strArray.length; i++) {
    if (!["+", "-", " "].includes(push(strArray[i]))){
       
}
  }

Hi @hiekamara1

The condition should not push anything.
Please remove that from the code, then the error message will show you what you need to code.

Happy coding

1 Like

hey Guys!
After four days of trial, I finally got the code right. Thanks to everyone of you guys for your most valuable time and help.

1 Like

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