From 28732eda9d687077680e2cbb4236f717b5bcf99e Mon Sep 17 00:00:00 2001 From: kosipov Date: Thu, 16 Oct 2025 19:10:44 +0300 Subject: [PATCH] add function for logic habit --- habit-manager.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/habit-manager.js b/habit-manager.js index f16ffbb..3122f94 100644 --- a/habit-manager.js +++ b/habit-manager.js @@ -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; + } +} +