${habit.longestStreak}
Longest Streak
`;
container.appendChild(card);
});
}
}
window.addHabit = function() {
const input = document.getElementById('habit-input');
const habitName = input.value.trim();
if (!habitName) {
alert('Please enter a habit name.');
return;
}
const newHabit = {
id: Date.now(),
name: habitName,
lastCompleted: null,
currentStreak: 0,
longestStreak: 0,
};
habits.push(newHabit);
saveHabits();
renderHabits();
input.value = '';
};
window.markComplete = function(id) {
const habit = habits.find(h => h.id === id);
if (!habit) return;
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
const todayStr = today.toISOString().split('T')[0];
const yesterdayStr = yesterday.toISOString().split('T')[0];
if (habit.lastCompleted === todayStr) {
// Already completed today, do nothing.
return;
} else if (habit.lastCompleted === yesterdayStr) {
// Streak continues
habit.currentStreak++;
} else {
// Streak is broken or it's the first time
habit.currentStreak = 1;
}
if (habit.currentStreak > habit.longestStreak) {
habit.longestStreak = habit.currentStreak;
}
habit.lastCompleted = todayStr;
saveHabits();
renderHabits();
};
window.deleteHabit = function(id) {
if (confirm("Are you sure you want to delete this habit? This cannot be undone.")) {
habits = habits.filter(h => h.id !== id);
saveHabits();
renderHabits();
}
};
window.downloadPDF = function() {
if (habits.length === 0) {
alert("There is no data to download.");
return;
}
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
doc.setFontSize(20);
doc.text("My Productivity Streak Report", 14, 22);
const tableBody = habits.map(habit => [
habit.name,
habit.currentStreak,
habit.longestStreak,
habit.lastCompleted || 'Never'
]);
doc.autoTable({
head: [['Habit', 'Current Streak', 'Longest Streak', 'Last Completed']],
body: tableBody,
startY: 30,
theme: 'grid',
headStyles: { fillColor: [79, 70, 229] } // Indigo
});
doc.save('productivity_streak_report.pdf');
};
// Initial Load
loadHabits();
});