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??
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?!
" 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.
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?
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.