Learn Form Validation by Building a Calorie Counter - Step 22

Tell us what’s happening:

I cannot find an error in this to save my life. Also I’m 99% sure it isn’t suppose to be if (!["+","-"," "].includes(strArray[i])).

I am 99% sure it should be if (!strArray.includes(["+","-"," "])). Is that correct or am I wrong? Either way, what will get the code to pass?

Your code so far

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

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

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 Edg/120.0.0.0

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 22

Make sure your curly brackets are in the right places

1 Like

Hey @craegan15 and welcome to the FCC community!

You have an extra curly brace above the for loop that is causing an error. All the rest of your code is correct!

1 Like

That bracket goes with the function cleanInputString

This bracket is causing issues because it ends the function and then you have a for loop that’s now outside of the function.

1 Like

I try to remove it and it still doesn’t work. I also try to put it after the for loop and it doesn’t work

Please post the updated code because moving it outside the for loop is what you should have done.

1 Like

I know but it still won’t work):
updated code:

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

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

This line is now incorrect for a couple reasons. There is nothing in the includes() method, and the order is reversed for what to check.

You should be checking the ["+","-"," "] to ensure that the current character is not a part of this array.

You had it correct here:

if (!["+","-"," "].includes(strArray[i])) {

But you changed it which is why it is no longer working. Remember, you want to check that the array containing the plus, minus, and space does not contain the current character we’re checking.

2 Likes