Learn Form Validation by Building a Calorie Counter - Step 74

Tell us what’s happening:

I have used use return to end the function execution but it doesn’t seem to work. Any ideas of what I’m doing wrong?

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

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){
  isError=true;
  return;
}
}

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 74

Welcome to the forum @cmuraya12

The function should only contain the keyword.

isError=true; is assigning a Boolean value. It is also not needed.

Happy coding

In your provided code snippet, it seems you are trying to use the return statement to exit the calculateCalories function if an error (isError ) is encountered. However, there are a couple of issues:

function calculateCalories(e) {
e.preventDefault();

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 budgetNumberInput = document.querySelector(‘#budget input[type=number]’); // Assuming budgetNumberInput is defined somewhere

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

// Check for errors
let isError = false;
// Example of a condition that sets isError to true
if (breakfastCalories < 0 || lunchCalories < 0 || dinnerCalories < 0 || snacksCalories < 0 || exerciseCalories < 0 || budgetCalories < 0) {
isError = true;
}

// If there’s an error, return to exit the function
if (isError) {
return;
}

// Continue with the rest of your function if no errors occur
}
In this revised version, I’ve added a condition to check for errors (arbitrarily assuming that a calorie value less than 0 indicates an error), and if an error is detected, the function returns, effectively stopping further execution.

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