%%html
<body>
    <script>
        function calculateCalories() {
            const age = parseFloat(document.getElementById('age').value);
            const weight = parseFloat(document.getElementById('weight').value);
            const height = parseFloat(document.getElementById('height').value);
            const activity = document.getElementById('activity').value;
            const gender = document.querySelector('input[name="gender"]:checked').value;
            const username = document.getElementById('username').value;

            const userData = {
                username: username,
                age: age,
                weight: weight,
                height: height,
                activity: activity,
                gender: gender
            };

            // Calculate the calorie maintenance on the client side
            const calorie_maintenance = calculateClientCalories(userData);
        }
    </script>
</body>
</html>
%%html
<body>
    <script>
function calculateClientCalories(userData) {
            // Implement your calorie calculation logic here on the client side
            // Replace this with your actual calculation
            const age = userData.age;
            const weight = userData.weight;
            const height = userData.height;
            const activity = userData.activity;
            const gender = userData.gender;

            // Define activity level multipliers
            const activityLevels = {
                sedentary: 1.2,
                lightly_active: 1.375,
                moderately_active: 1.55,
                very_active: 1.725
            };

            // Example calorie calculation (Harris-Benedict equation)
            if (gender === 'male') {
                return (88.362 + 13.397 * weight + 4.799 * height - 5.677 * age) * activityLevels[activity];
            } else {
                return (447.593 + 9.247 * weight + 3.098 * height - 5.677 * age) * activityLevels[activity];
            }
        }
    </script>
</body>

frontend code

  • It takes the data inputted from the form and stores them as constants for the calculations
  • It then uses the constants gathered to calculate the client’s calorie maintenence using the Harris Benedict Equation which we found, that uses the inputs to calculate daily calorie maintenence
  • Originally in frontend, but later moved to backend so that the data for the calorie maintenence is stored along with the user’s information into the database