JSFiddle - React, Tailwind, and code Playground
by guydog28
HTML
<div class="container">
<header>
<h1>Weight Forecast Calculator</h1>
</header>
<div id="description">
<p><strong>This form is for estimating how caloric intake and exercise (or lack thereof) will affect your weight over time</strong></p>
<p>Modify the values in the form below and watch how the results change under it.</p>
<p><strong>Notes:</strong></p>
<ul>
<li>For multiplier, choose an estimated value based on these:
<ul>
<li>Sedentary (little or no exercise): <strong>1.2</strong>
<ul>
<li>This level might apply to someone who does light activity for daily living but does not engage in any formal exercise and may have a desk job.</li>
</ul>
</li>
<li>Lightly active (light exercise/sports 1-3 days/week): <strong>1.375</strong>
<ul>
<li>This could be someone who does a little light exercise, such as walking or gentle yoga, a few times a week.</li>
</ul>
</li>
<li>Moderately active (moderate exercise/sports 3-5 days/week): <strong>1.55</strong>
<ul>
<li>This level would apply to someone who engages in moderate activity like brisk walking, dancing, swimming, or cycling most days of the week.</li>
</ul>
</li>
<li>Very active (hard exercise/sports 6-7 days a week): <strong>1.725</strong>
<ul>
<li>This would be someone who does more intense exercise like running, fast cycling, or competitive sports almost every day.</li>
</ul>
</li>
<li>Extra active (very hard exercise/sports & a physical job or 2x training): <strong>1.9</strong>
<ul>
<li>This level would apply to someone who does intense physical exercise multiple times a day or has a physically demanding job, like construction work or...
CSS
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
padding: 20px;
color: #333;
}
.container {
background-color: #fff;
max-width: 700px;
margin: auto;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
.container h1 {
text-align: center;
margin-bottom: 40px;
color: #333;
}
.row {
display: flex;
flex-wrap: wrap;
margin-bottom: 15px;
justify-content: space-between;
}
.inputGroup {
flex: 1;
padding: 0 10px;
margin-bottom: 20px;
}
.inputGroup label {
display: block;
font-weight: bold;
margin-bottom: 10px;
}
.inputGroup input,
.inputGroup select {
width: 100%;
padding: 8px;
border-radius: 5px;
border: 1px solid #ddd;
font-size: 14px;
}
.inputGroup input:focus,
.inputGroup select:focus {
outline: none;
border-color: #aaa;
}
#results {
margin-top: 30px;
padding: 20px;
background-color: #f0f0f0;
border-radius: 5px;
display: none;
}
.output {
font-size: 16px;
font-weight: normal;
}
#calculate {
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
background-color: #28a745;
color: white;
font-size: 16px;
cursor: pointer;
margin-top: 20px;
}
#calculate:hover {
background-color: #218838;
}
#calculate:focus {
outline: none;
}
.description {
margin-bottom: 20px;
line-height: 1.6;
color: #555;
}
JavaScript
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('startDateInput').valueAsDate = new Date();
document.getElementById('calculate').addEventListener('click', function() {
const startDate = new Date(document.getElementById('startDateInput').value);
// Get values from the form
const sex = document.getElementById('sexInput').value;
const birthday = new Date(document.getElementById('birthdayInput').value);
const heightFt = parseInt(document.getElementById('heightFtInput').value);
const heightIn = parseFloat(document.getElementById('heightInInput').value);
const weight = parseFloat(document.getElementById('weightInput').value);
const calories = parseInt(document.getElementById('caloriesInput').value);
const multiplier = parseFloat(document.getElementById('multiplierInput').value);
const outputDays = parseInt(document.getElementById('outputDaysInput').value);
const forecastDays = parseInt(document.getElementById('forecastDaysInput').value);
const ageNow = (startDate-birthday)/(1000*60*60*24*365);
// Convert height to centimeters
const heightCm = (heightFt * 12 + heightIn) * 2.54;
// Convert weight to kilograms
let currentWeightKg = weight * 0.453592; // let, because this will change
// Prepare results output
let resultsOutput = '';
// Loop through each day
for(let days = 1; days <= forecastDays; days++) {
// Calculate the age incrementally for each day
const age = ageNow + days / 365; // This assumes a year is always 365 days
// Calculate current TDEE
const tdee = multiplier * (10 * currentWeightKg + 6.25 * heightCm - 5 * age + (sex === 'female' ? -161 : 5));
// Calculate weight change for the day, in kg
const weightChangeKg = (calories - tdee) / 7700; // 1 kg is approx....