This application is designed to help you track your daily caloric intake and manage your dietary goals with ease.
The Calorie Counter App is a web-based tool developed using HTML, CSS, and JavaScript. It allows users to input their daily caloric budget and log their meals and exercises throughout the day. The app then calculates the remaining calories based on the inputs provided, helping users stay on track towards their health and fitness goals.
The Calorie Counter App's JavaScript code consists of several functions and event listeners that handle user interactions, input validation, and calculations. Here's a breakdown of the key JavaScript logic:
The JavaScript code starts by selecting various HTML elements from the DOM using document.getElementById() and document.querySelector(). Event listeners are then attached to specific elements to trigger actions when certain events occur, such as clicking buttons or submitting forms.
Two main functions handle input validation:
-
cleanInputString(str): This function removes any non-numeric characters from a string, ensuring that only valid numeric inputs are processed.
-
isInvalidInput(str): This function checks if a string contains invalid input, such as exponential notation (e.g., 1e3). If invalid input is detected, an alert is displayed to notify the user.
The addEntry() function is called when the user clicks the "Add Entry" button. It dynamically generates HTML input fields for the selected entry type (e.g., breakfast, lunch) and appends them to the corresponding fieldset container in the form.
The calculateCalories(e) function is invoked when the user submits the form to calculate remaining calories. This function retrieves input values from various HTML input elements, calculates consumed and remaining calories, and updates the output display accordingly.
- getCaloriesFromInputs(list): This function iterates over a list of HTML input elements, extracts their values, performs input validation, and calculates the total calories. It returns the sum of valid input values or null if invalid input is detected.
The clearForm() function resets the form by clearing all input fields and hiding the output display.
To play the game, simply open the index.html file in your web browser. You can also follow this link: https://calorie-counter-by-anabeeella.netlify.app/