Advanced Intermittent Fasting Calculator
🎯 Your Personalized Fasting Plan
📅 Your Daily Fasting Schedule
🧬 Expected Health Benefits
📊 Fasting Progress Tracker
Not Started
🕐 Eating Window: ${firstMeal} - ${lastMeal}
🚫 Fasting Period: ${lastMeal} - ${firstMeal} (next day)
`;
}// Main calculation function
function calculateFasting() {
const currentWeight = parseFloat(document.getElementById('currentWeight').value);
const targetWeight = parseFloat(document.getElementById('targetWeight').value);
const height = parseFloat(document.getElementById('height').value);
const age = parseFloat(document.getElementById('age').value);
const gender = document.getElementById('gender').value;
const activityLevel = document.getElementById('activityLevel').value;
const goal = document.getElementById('fastingGoal').value;
// Check if all required fields are filled
if (!currentWeight || !targetWeight || !height || !age) {
return; // Don't calculate if required fields are missing
}
// Calculate BMR and TDEE
const bmr = calculateBMR(currentWeight, height, age, gender);
const tdee = calculateTDEE(bmr, activityLevel);
// Calculate weight loss parameters
const weightToLose = currentWeight - targetWeight;
const recommendedWeeklyLoss = Math.min(weightToLose * 0.1, 1); // Max 1kg per week
const weeksToGoal = Math.ceil(weightToLose / recommendedWeeklyLoss);
const dailyCalorieDeficit = recommendedWeeklyLoss * 7700 / 7; // 7700 cal per kg
// Get method-specific benefits
const benefits = getMethodBenefits(fastingData.selectedMethod, goal);
// Update results
document.getElementById('recommendedMethod').textContent = fastingData.selectedMethod;
document.getElementById('weightLossRate').textContent = recommendedWeeklyLoss.toFixed(1);
document.getElementById('targetDuration').textContent = weeksToGoal;
document.getElementById('calorieDeficit').textContent = Math.round(dailyCalorieDeficit);
// Update benefits
updateBenefitsGrid(benefits);
// Update timeline
updateFastingSchedule();
// Show results
document.getElementById('result').style.display = 'block';
}// Get benefits based on method and goal
function getMethodBenefits(method, goal) {
const benefitsData = {
weight_loss: [
{ title: "Fat Burning", desc: "Enhanced lipolysis during fasting periods" },
{ title: "Insulin Sensitivity", desc: "Improved glucose metabolism" },
{ title: "Metabolic Flexibility", desc: "Better fat oxidation capacity" }
],
autophagy: [
{ title: "Cellular Cleanup", desc: "Removal of damaged proteins and organelles" },
{ title: "DNA Repair", desc: "Enhanced cellular repair mechanisms" },
{ title: "Longevity Genes", desc: "Activation of SIRT1 and other longevity pathways" }
],
metabolic_health: [
{ title: "Blood Sugar Control", desc: "Stabilized glucose levels" },
{ title: "Insulin Sensitivity", desc: "Reduced insulin resistance" },
{ title: "Lipid Profile", desc: "Improved cholesterol ratios" }
],
mental_clarity: [
{ title: "BDNF Production", desc: "Increased brain-derived neurotrophic factor" },
{ title: "Ketone Bodies", desc: "Alternative brain fuel for clarity" },
{ title: "Neuroplasticity", desc: "Enhanced brain adaptability" }
]
};
return benefitsData[goal] || benefitsData.weight_loss;
}// Update benefits grid
function updateBenefitsGrid(benefits) {
const benefitsGrid = document.getElementById('benefitsGrid');
if (!benefitsGrid) return;
benefitsGrid.innerHTML = benefits.map(benefit => `${benefit.title}
${benefit.desc}
`).join('');
}// Timer functions
function startFast() {
fastingData.fastStartTime = new Date();
document.getElementById('startFastBtn').style.display = 'none';
document.getElementById('endFastBtn').style.display = 'inline-block';
// Start timer
fastingData.fastTimer = setInterval(updateFastTimer, 1000);
}function endFast() {
if (fastingData.fastTimer) {
clearInterval(fastingData.fastTimer);
fastingData.fastTimer = null;
}
fastingData.fastStartTime = null;
document.getElementById('startFastBtn').style.display = 'inline-block';
document.getElementById('endFastBtn').style.display = 'none';
document.getElementById('fastTimer').textContent = 'Not Started';
// Update streak (simple increment for demo)
const currentStreak = parseInt(document.getElementById('fastingStreak').textContent);
document.getElementById('fastingStreak').textContent = currentStreak + 1;
}function updateFastTimer() {
if (!fastingData.fastStartTime) return;
const now = new Date();
const elapsed = now - fastingData.fastStartTime;
const hours = Math.floor(elapsed / (1000 * 60 * 60));
const minutes = Math.floor((elapsed % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((elapsed % (1000 * 60)) / 1000);
document.getElementById('fastTimer').textContent =
`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}// Update recommendations based on goal
function updateRecommendations() {
const goal = document.getElementById('fastingGoal').value;
const methodRecommendations = {
weight_loss: '16:8',
autophagy: '18:6',
metabolic_health: '16:8',
longevity: '20:4',
muscle_gain: '16:8',
mental_clarity: '18:6'
};
const recommendedMethod = methodRecommendations[goal] || '16:8';
selectMethod(recommendedMethod);
}// Initialize the calculator
document.addEventListener('DOMContentLoaded', function() {
// Set default method as selected
selectMethod('16:8');
// Add sample values for testing
document.getElementById('currentWeight').value = '80';
document.getElementById('targetWeight').value = '75';
document.getElementById('height').value = '175';
document.getElementById('age').value = '30';
// Auto-calculate with sample values
setTimeout(() => {
calculateFasting();
}, 500);
});