// This is the calorie burn rate per minute formula used in the calculator
// 0.0175 * MET * weight in kg
function calorieBurnRatePerMinute(intensity, weight) {
// Look up the MET value for the given intensity using a table of physical activities and their corresponding MET values
// For example, the MET value for jogging at a moderate pace is 8.0
const MET = 8.0;
return 0.0175 * MET * weight;
}
// This function calculates the total number of calories burned based on the duration and intensity of the activity
function calculateCaloriesBurned(duration, intensity, weight) {
const calorieBurnRatePerMinute = calorieBurnRatePerMinute(intensity, weight);
// The total number of calories burned is the burn rate per minute multiplied by the duration of the activity in minutes
return calorieBurnRatePerMinute * duration;
}
// This is the widget that users can interact with
function createCalorieCalculatorWidget() {
// Create the necessary HTML elements for the widget
const widget = document.createElement('div');
const durationLabel = document.createElement('label');
const durationInput = document.createElement('input');
const intensityLabel = document.createElement('label');
const intensityInput = document.createElement('input');
const weightLabel = document.createElement('label');
const weightInput = document.createElement('input');
const submitButton = document.createElement('button');
const resultLabel = document.createElement('p');
// Set the text and attributes of the HTML elements
durationLabel.innerHTML = 'Duration (minutes):';
durationInput.type = 'number';
intensityLabel.innerHTML = 'Intensity:';
intensityInput.type = 'text';
weightLabel.innerHTML = 'Weight (kg):';
weightInput.type = 'number';
submitButton.innerHTML = 'Calculate';
resultLabel.innerHTML = 'Result:';
// Add the HTML elements to the widget container
widget.appendChild(durationLabel);
widget.appendChild(durationInput);
widget.appendChild(intensityLabel);
widget.appendChild(intensityInput);
widget.appendChild(weightLabel);
widget.appendChild(weightInput);
widget.appendChild(submitButton);
widget.appendChild(resultLabel);
// Add an event listener to the submit button that calculates the number of calories burned when clicked
submitButton.addEventListener('click', () => {
const duration = durationInput.value;
const intensity = intensityInput.value;
const weight = weightInput.value;
const caloriesBurned = calculateCaloriesBurned(duration, intensity, weight);
resultLabel.innerHTML = `Result: ${caloriesBurned} calories`;
});
return widget;
}