Learn Form Validation by Building a Calorie Counter - Step 22

Tell us what’s happening: I am really stuck on this step. I tried every solution I could think of but I still can’t pass. Please help.

Step 22

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.

The .includes() method returns true if the array contains the character, and false if not. The logical NOT operator (!) will return the opposite of the value of the .includes() method.

Here is an example:

const numbersArray = [1, 2, 3, 4, 5]
const number = 6

if (!numbersArray.includes(number)) {
  console.log("The number is not in the array.")
}

Your code so far

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

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

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

/** 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])
}
}
} **/ This code work for me, try it yourself

I just figured it out. My solutions were correct. All I had to do was delete all my comments and have the if statement close to the for opening bracket.

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])
    }
  }
}