Learn Form Validation by Building a Calorie Counter - Step 76

Tell us what’s happening:

This is the lesson that may end my journey in coding. I don’t think I can comprehend this. Help if you want, I’m stck. "
if (isError = true) {return}

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

function calculateCalories(e) {
  e.preventDefault();
  isError = false;

  const breakfastNumberInputs = document.querySelectorAll('#breakfast input[type=number]');
  const lunchNumberInputs = document.querySelectorAll('#lunch input[type=number]');
  const dinnerNumberInputs = document.querySelectorAll('#dinner input[type=number]');
  const snacksNumberInputs = document.querySelectorAll('#snacks input[type=number]');
  const exerciseNumberInputs = document.querySelectorAll('#exercise input[type=number]');

  const breakfastCalories = getCaloriesFromInputs(breakfastNumberInputs);
  const lunchCalories = getCaloriesFromInputs(lunchNumberInputs);
  const dinnerCalories = getCaloriesFromInputs(dinnerNumberInputs);
  const snacksCalories = getCaloriesFromInputs(snacksNumberInputs);
  const exerciseCalories = getCaloriesFromInputs(exerciseNumberInputs);
  const budgetCalories = getCaloriesFromInputs([budgetNumberInput]);

  if (isError = true) {
    return;
  }
}

// User Editable Region

Your browser information:

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

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 76

Welcome to the forum @demegilb1

Have a look at the code you wrote earlier.

Happy coding

Welcome to the FCC forum. isError is already initialize to true in your code.

@demegilb1

i do not understand, what you are trying to explain

i said isError is asigned already to true in your script.js file. you did not need to equal it within the if condition.
@demegilb1

The question was asking me to check the “truthiness” of the global flag error. I thought this meant plugging (mind the novice wording) a Boolean in the condition. I appreciate your help. But honestly, still don’t get the concept 100%.

So it’s called “boolean coercion” you can read more about it here. Basically, when you have a bit of code that normally has a boolean context (that is, anywhere you would normally see “true” or “false”, or an expression that evaluates to “true” or “false” - for example (isError = true)), you can actually put basically any type of data like strings, or numbers. Javascript will attempt to evaluate those values as if they were booleans. “Falsy” values are other data types that Javascript will evaluate as “false” - for example an empty string, or the number 0, or NULL. A list of all “falsy” values is here.. “Truthy” values are any values that are not “falsy”. It’s essentially asking the question does this data contain anything meaningful (truthy), or is it empty (falsy)?

One way you might use this kind of logic is to test whether you received any input.

let input = document.getElementById("<some_input_element_a_user_can_type_in>")
let myString = "";
myString += input.value;
if (myString) {
  <do something>
}

myString is not a boolean, which is expected in an if block, but you can use it like it were. If the user entered anything into the input element, the string would be non-empty, which is “truthy”, and the <do something> code would run. If the user did not enter anything, the string would be an empty string, which is “falsy”, and that code will not run.

Hope this helps, stick it with it don’t give up :slight_smile:

2 Likes