Sleep Debt Calculator

Sleep Debt Calculator

Find out if you're getting the sleep you need.

This determines your recommended nightly sleep.

You have a sleep debt of

${results.sleepDebt.toFixed(1)}

hours this week.

This suggests you're not getting enough sleep. Prioritizing rest can improve your health and well-being.

`; } else { resultHTML = `

Congratulations!

${Math.abs(results.sleepDebt).toFixed(1)}

hour sleep surplus.

You're meeting or exceeding your sleep needs. Keep up the great habits!

`; } resultsDisplay.innerHTML = resultHTML; resultsSection.classList.remove('hidden'); } // --- PDF DOWNLOAD LOGIC --- async function downloadPDF() { if (resultsSection.classList.contains('hidden')) { alert("Please calculate your sleep debt first before downloading."); return; } pdfLoader.classList.remove('hidden'); pdfDownloadBtn.disabled = true; const { jsPDF } = window.jspdf; const pdfExportContent = document.getElementById('pdf-export-area'); const results = getCalculationResults(); const ageGroupText = ageGroupSelect.options[ageGroupSelect.selectedIndex].text; let dayRows = ''; for (let i = 0; i < 7; i++) { dayRows += ` ${last7Days[i]} ${results.actualSleepPerDay[i].toFixed(1)} hours `; } pdfExportContent.innerHTML = `

Sleep Debt Report

Generated on: August 2, 2025

Your Weekly Sleep Log

${dayRows}
Day Hours Slept
Total Actual Sleep ${results.totalActual.toFixed(1)} hours

Your Age Group

${ageGroupText}

Recommended Weekly Sleep

${results.totalRecommended.toFixed(1)} hours

${results.sleepDebt > 0 ? 'Your Sleep Debt' : 'Your Sleep Surplus'}

${Math.abs(results.sleepDebt).toFixed(1)}

hours

`; await new Promise(resolve => setTimeout(resolve, 100)); try { const canvas = await html2canvas(pdfExportContent, { scale: 2 }); const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: "portrait", unit: "pt", format: "a4" }); const pageWidth = pdf.internal.pageSize.getWidth(); const margin = 40; const imgProps = pdf.getImageProperties(imgData); const pdfImageWidth = pageWidth - (margin * 2); const pdfImageHeight = (imgProps.height * pdfImageWidth) / imgProps.width; pdf.addImage(imgData, 'PNG', margin, margin, pdfImageWidth, pdfImageHeight); pdf.save(`Sleep-Debt-Report-${new Date().toISOString().slice(0,10)}.pdf`); } catch (error) { console.error("Error generating PDF:", error); alert("Sorry, there was an error creating the PDF."); } finally { pdfLoader.classList.add('hidden'); pdfDownloadBtn.disabled = false; pdfExportContent.innerHTML = ''; } } // --- START THE APP --- initialize(); });
Scroll to Top