add function for logic habit

This commit is contained in:
2025-10-16 19:10:44 +03:00
parent 62e069592a
commit 28732eda9d

View File

@@ -19,7 +19,10 @@ HabitManager.createHabit = function (name, description, targetCount = 1) {
id: Math.random(),
name: name,
description: description,
targetCount: targetCount
targetCount: targetCount,
completions: [],
currentCount: 0,
isCompleted: false
}
this.habits.push(habit)
@@ -27,3 +30,50 @@ HabitManager.createHabit = function (name, description, targetCount = 1) {
return habit
}
HabitManager.markHabitDone = function (habitId) {
const habit = this.habits.find(h => h.id === habitId);
if (!habit) {
console.error('Привычка не найдена!');
return null;
}
const today = new Date().toDateString();
if (habit.completions.includes(today)) {
alert('Вы уже выполняли эту привычку сегодня!');
return habit;
}
habit.completions.push(today);
habit.currentCount++;
if (habit.currentCount >= habit.targetCount) {
habit.isCompleted = true;
}
// todo далее доработать пересчет статистики
return habit;
}
HabitManager.saveToLocalStorage = function () {
const data = {
habits: this.habits,
settings: this.settings,
stats: this.stats
};
localStorage.setItem('habitTrackerData', JSON.stringify(data));
}
HabitManager.loadFromLocalStorage = function () {
const data = localStorage.getItem('habitTrackerData');
if (data) {
const parsedData = JSON.parse(data);
this.habits = parsedData.habits;
this.settings = parsedData.settings;
this.stats = parsedData.stats;
}
}