code a calorie tracker. I want an nice looking interface. make sure that the calorie…
- User
code a calorie tracker. I want an nice looking interface. make sure that the calorie calculation is correct -> fat, carbs and proteins have their own calorie score and I want that the user can enter calorie, fat, carbs and proteins . therefore if the user puts more fat in the score the combined calorie of the meal should go up. implement a slider of the percentage of proteins, carbs and fat that the user can use after entering a calorie score
- Assistant
Here’s a Calorie Tracker web app written in HTML + CSS + JavaScript with a clean and interactive interface. It:
- Allows users to enter calories, fat, carbs, and proteins.
- Calculates if the entered macronutrients match the given calorie value.
- Allows users to adjust macronutrient percentage distribution using sliders, and recalculates the grams of each based on the target calorie.
- Uses correct calorie values:
- Fat: 9 kcal/g
- Carbohydrate: 4 kcal/g
- Protein: 4 kcal/g
✅ Core Features
- User input for calorie, fat (g), carbs (g), protein (g)
- Displays computed total calories from macros
- Displays difference between entered and calculated values
- Sliders allow adjusting macro ratio (adds up to 100%)
- When sliders move, adjust grams based on calorie input and macro % values
🧠 Code Explanation
- Sliders control percentage of daily caloric intake:
- e.g., 40% carbs → (40% of total calories) / 4 (kcal/g) → grams
- Instant feedback on consistency of input
🖥️ Full Code (HTML + CSS + JS)
Save this as an
index.htmlfile and open in your browser.<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <title>Calorie Tracker</title> <style> body { font-family: "Segoe UI", sans-serif; background: #f4f4f4; margin: 0; padding: 2rem; } .tracker { background: #fff; max-width: 700px; margin: auto; padding: 2rem; border-radius: 10px; box-shadow: 0 4px 16px rgba(0,0,0,0.1); } .tracker h1 { text-align: center; margin-bottom: 1.5rem; color: #333; } label { display: block; margin-top: 1rem; font-weight: 600; color: #555; } input[type="number"] { width: 100%; padding: 8px; margin-top: 5px; font-size: 1rem; border: 1px solid #ccc; border-radius: 6px; } .result { margin-top: 1.5rem; padding: 1rem; background: #e8f5e9; border-left: 6px solid #4caf50; border-radius: 6px; font-size: 1rem; } .sliders { margin-top: 2rem; background: #f8f8f8; padding: 1rem; border-radius: 8px; border: 1px solid #ddd; } .slider-container { margin-bottom: 1.5rem; } .slider-label { display: flex; justify-content: space-between; font-weight: 500; margin-bottom: 0.3rem; } input[type="range"] { width: 100%; } .macro-output { font-weight: 600; margin-top: 0.5rem; } .error { background: #ffebee; color: #c62828; padding: 1rem; border-left: 6px solid #c62828; border-radius: 6px; margin-top: 1rem; } </style> </head> <body> <div class="tracker"> <h1>Calorie Tracker</h1> <label for="calories">Total Calories:</label> <input type="number" id="calories" placeholder="e.g. 500" min="0"> <label for="fat">Fat (g):</label> <input type="number" id="fat" placeholder="e.g. 10" min="0"> <label for="carbs">Carbs (g):</label> <input type="number" id="carbs" placeholder="e.g. 50" min="0"> <label for="protein">Protein (g):</label> <input type="number" id="protein" placeholder="e.g. 20" min="0"> <div id="feedback" class="result"></div> <div class="sliders"> <h3>Adjust Macronutrient %</h3> <div class="slider-container"> <div class="slider-label"> <span>Protein: <span id="proteinPercent">30%</span></span> </div> <input type="range" id="proteinSlider" min="0" max="100" value="30"> <div class="macro-output" id="proteinGrams"></div> </div> <div class="slider-container"> <div class="slider-label"> <span>Carbs: <span id="carbPercent">40%</span></span> </div> <input type="range" id="carbSlider" min="0" max="100" value="40"> <div class="macro-output" id="carbGrams"></div> </div> <div class="slider-container"> <div class="slider-label"> <span>Fat: <span id="fatPercent">30%</span></span> </div> <input type="range" id="fatSlider" min="0" max="100" value="30"> <div class="macro-output" id="fatGrams"></div> </div> </div> </div> <script> const caloriesInput = document.getElementById('calories'); const fatInput = document.getElementById('fat'); const carbsInput = document.getElementById('carbs'); const proteinInput = document.getElementById('protein'); const feedback = document.getElementById('feedback'); const fatSlider = document.getElementById('fatSlider'); const carbSlider = document.getElementById('carbSlider'); const proteinSlider = document.getElementById('proteinSlider'); const fatPercent = document.getElementById('fatPercent'); const carbPercent = document.getElementById('carbPercent'); const proteinPercent = document.getElementById('proteinPercent'); const fatGrams = document.getElementById('fatGrams'); const carbGrams = document.getElementById('carbGrams'); const proteinGrams = document.getElementById('proteinGrams'); function updateFeedback() { const fat = parseFloat(fatInput.value) || 0; const carbs = parseFloat(carbsInput.value) || 0; const protein = parseFloat(proteinInput.value) || 0; const enteredCalories = parseFloat(caloriesInput.value) || 0; const calculatedCalories = (fat * 9) + (carbs * 4) + (protein * 4); const diff = Math.round(calculatedCalories - enteredCalories); feedback.innerHTML = ` <strong>Macro Calories:</strong> ${Math.round(calculatedCalories)} kcal<br> <strong>Difference from Entered:</strong> ${diff} kcal `; feedback.className = Math.abs(diff) > 10 ? 'error' : 'result'; } function updateSliders() { calories = parseFloat(caloriesInput.value) || 0; // Normalize sliders to 100% let total = parseInt(proteinSlider.value) + parseInt(carbSlider.value) + parseInt(fatSlider.value); if (total !== 100) { let factor = 100 / total; proteinSlider.value = Math.round(proteinSlider.value * factor); carbSlider.value = Math.round(carbSlider.value * factor); fatSlider.value = 100 - proteinSlider.value - carbSlider.value; } fatPercent.textContent = `${fatSlider.value}%`; carbPercent.textContent = `${carbSlider.value}%`; proteinPercent.textContent = `${proteinSlider.value}%`; const proteinCalories = (calories * proteinSlider.value) / 100; const carbCalories = (calories * carbSlider.value) / 100; const fatCalories = (calories * fatSlider.value) / 100; proteinGrams.textContent = `= ${Math.round(proteinCalories / 4)} g protein`; carbGrams.textContent = `= ${Math.round(carbCalories / 4)} g carbs`; fatGrams.textContent = `= ${Math.round(fatCalories / 9)} g fat`; } [fatInput, carbsInput, proteinInput, caloriesInput].forEach(el => { el.addEventListener('input', () => { updateFeedback(); updateSliders(); }); }); [fatSlider, carbSlider, proteinSlider].forEach(slider => { slider.addEventListener('input', () => { updateSliders(); }); }); window.addEventListener('DOMContentLoaded', () => { updateFeedback(); updateSliders(); }); </script> </body> </html>
🧪 How It Works
- Enter the total calories, fat, carbs, or proteins in grams.
- The app dynamically calculates calories from macros on each input.
- If macros do not add up to total calories, it warns with a red box.
- Sliders allow tweaking target macro % (must total 100%), showing equivalent grams for that percent.
📌 You Can Extend This With:
- Saving meals using
localStorage - Graph chart of macro distribution (e.g., pie chart)
- Dark mode toggle
- Nutrition API integration (e.g., USDA) for food data
Would you like it as a React app or mobile version too?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.