A null array for if condition? by Building a Calorie Counter - Step 60

How can they pass an array to if??

function cleanInputString(str) {
  const regex = /[+-\s]/g;
  return str.replace(regex, '');
}

function isInvalidInput(str) {
  const regex = /\d+e\d+/i;
  return str.match(regex);
}
//my code starts here
function getCaloriesFromInputs(list) {
  let calories = 0;
  for (const item of list) {
    const currVal = cleanInputString(item.value);
    const invalidInputMatch = isInvalidInput(currVal);
    if (invalidInputMatch) {}    
  }
}

The isInvalidInput () function returns an array of null (if valid input, no “e” is used) or with the invalid e, ie. no boolean value. So why is the correct answer: if (invalidInputMatch) {} ?
What is a null array anyway? What type is it that has a True/False value that my IF can use??

Reference: java - When should null values of Boolean be used? - Stack Overflow

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

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 61

Hello, do you know how to call a function, because thats the first thing going in the if conditonal block. Inside the parenthese is going to be an argument. Try using your console to get feedback for this challange.

I have the answer if (invalidInputMatch) {} but it doesn’t make sense to me. I can’t go on the next step without understanding how on earth this condition is possible?!

Are you saying it passes as is? Anyway I thought your were on question 61.

My code in the question is the model answer. Can you please explain what thing is invalidInputMatch that goes inside If condition?

You need to start with a function call function()

But that’s how the code passes step 60, no need for any change.

Yes no more code on that challange.

but how is that possible? an array or non-existent array passed in If condition? Is the answer wrong?

I dont see any mention of an array on this challange. The if conditional is a variable.

" Remember that your isInvalidInput function returns String.match, which is an array of matches or null if no matches are found.

In JavaScript, values can either be truthy or falsy. A value is truthy if it evaluates to true when converted to a Boolean. A value is falsy if it evaluates to false when converted to a Boolean. null is an example of a falsy value.

You need to check if invalidInputMatch is truthy – you can do this by passing the variable directly to your if condition"

If the invalidInputMatch is a valid array, it wouldn’t have a truthy/falsy value, either because it is just an array? Similarly if I say a basket of fruit is not true nor false, it is just a basket.

I see your point but it takes time to understand some of these concepts.

The code is going to find matches over several arrays, if none its going to come back null which means no match was found.

The expected solution is correct. An empty array acts like the value false when treated as a Boolean and a non-empty array acts like the value true when treated as a Boolean.

Is this the same for list or everything similar to array, ie. to be false when it is null and vice versa? Is this rule new or accepted in all versions of JS and other related things?

The concept of truthy and falsy values is at least 70 years old. Every data type has truthy and falsy values.

Wow, this step is the most eye-opening lesson I’ve learned so far! Thank you!!!

1 Like

To call a function, simply use the function name followed by parentheses, and pass the argument inside the parentheses. For example, functionName(argument). Use the console to test and get feedback.