Learn Form Validation by Building a Calorie Counter - Step 22

Tell us what’s happening:

I can’t understand what’s wrong. The documentation for includes doesn’t mention searching arrays :confused:

### Your code so far

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

  for (let i = 0; i < strArray.length; i++) {
    if(!strArray[i].includes(["+", "-", " "])){
      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 Edg/120.0.0.0

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 22

It is the other way. You need to call includes on the ["+", "-", " "] array and pass it strArray[i]

const numbers = [1, 'two', 3, 'four', 5, 6, 'seven', 8, 9, 10];
const notNumbers = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];

const filtered = [];

for(const number of numbers) {
  if(!notNumbers.includes(number)) {
    filtered.push(number);
  }
}

console.log(filtered); // [1, 3, 5, 6, 8, 9, 10]

You have this backwards. Look at the example again:

if (!numbersArray.includes(number)) {

Notice that the array is on the left side of .includes(). That’s because includes is a method of an Array and thus you can only call it on an array (or things that act like arrays). The value you are testing to see if it is included in the array is passed into the includes method. You can translate the example code:

numbersArray.includes(number)

Into “Does the numbersArray include number.” This will return either true or false. And then adding ! to the beginning gives you the opposite of true or false.

Based on the above, do you think you can fix your code so that you are checking if each character in strArray is included in the ["+", "-", " "] array?

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