Learn Form Validation by Building a Calorie Counter - Step 65

Tell us what’s happening:

I am not getting the answer as i have tried many ways

Your code so far

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

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

}function getCaloriesFromInputs(list) {
  let calories = 0; // Initialize total calories to 0

  // Loop through each item in the provided list
  for (const item of list) {
    const currVal = cleanInputString(item.value); // Clean the input string
    const invalidInputMatch = isInvalidInput(currVal); // Check for invalid input

    // If there's an invalid input, alert and return null
    if (invalidInputMatch) {
      alert(`Invalid Input: ${invalidInputMatch[0]}`);
      return null; // Exit if there's an invalid input
    
    Number('currVal')+=calories;
    }
    // Convert currVal to a number
    const numericValue = Number(currVal);

    // Check if numericValue is a valid number
    if (isNaN(numericValue)) {
      alert(`Invalid Input: '${currVal}' is not a valid number.`);
      return null; // Exit if the conversion to number fails
    }

    // Use addition assignment operator to add the numeric value to total calories
    calories += numericValue; // Add numericValue to calories
  }

  return calories; // Return the total calories
}





// User Editable Region
/* file: styles.css */

Your browser information:

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

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 65

Key Issues and Suggestions:

  1. Incorrect Number('currVal')+=calories;: This line is syntactically incorrect because you’re converting the string 'currVal' (which is the literal string, not the variable) into a number. You should be adding numericValue to calories instead.
  2. Proper Input Handling: After cleaning the input string (cleanInputString(item.value)), you want to ensure it can be correctly converted into a number before processing.
  3. Invalid Input Handling: You are correctly checking for invalid input and using isInvalidInput(). However, your logic should continue if the input is valid and break only if the input is not valid.

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