-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
120 lines (98 loc) · 3.02 KB
/
Copy pathscript.js
File metadata and controls
120 lines (98 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Eco-friendly tips
const tips = [
"💧 Save water by turning off taps when not in use!",
"♻️ Recycle plastic, paper, and glass to reduce waste!",
"🌳 Plant a tree and help combat climate change!",
"🚴 Use a bicycle instead of a car to reduce emissions!",
"💡 Switch to LED bulbs and save energy!",
"🥕 Eat more plant-based meals for a greener planet!",
"🛍️ Use reusable bags instead of single-use plastic!"
];
// Load saved habits from local storage
let habits = JSON.parse(localStorage.getItem("habits")) || [];
// Add a new habit
function addHabit() {
const input = document.getElementById("habitInput");
const habitText = input.value.trim();
if (habitText === "") return;
const habit = { text: habitText, done: false, streak: 0 }; // added streak
habits.push(habit);
input.value = "";
saveHabits();
renderHabits();
}
// Toggle habit complete/incomplete
function toggleHabit(index) {
const habit = habits[index];
habit.done = !habit.done;
if (habit.done) {
habit.streak += 1; // increase streak
showTip(); // show eco tip
} else {
habit.streak = 0; // reset streak
}
saveHabits();
renderHabits();
}
// Delete a habit
function deleteHabit(index) {
habits.splice(index, 1); // remove from array
saveHabits();
renderHabits();
}
// Show random eco tip
function showTip() {
const tip = tips[Math.floor(Math.random() * tips.length)];
document.getElementById("tipText").textContent = tip;
}
// Save habits to local storage
function saveHabits() {
localStorage.setItem("habits", JSON.stringify(habits));
}
// Render habit list
function renderHabits() {
const list = document.getElementById("habitList");
list.innerHTML = "";
let completed = 0;
habits.forEach((habit, index) => {
const li = document.createElement("li");
li.textContent = habit.text;
if (habit.done) {
li.classList.add("completed");
completed++;
}
// Streak display
if (habit.streak > 0) {
li.textContent += ` 🔥 ${habit.streak}-day streak`;
}
// Click to toggle completion
li.onclick = () => toggleHabit(index);
// Delete button
const delBtn = document.createElement("button");
delBtn.textContent = "🗑️";
delBtn.onclick = (e) => {
e.stopPropagation(); // prevent toggle
deleteHabit(index);
};
li.appendChild(delBtn);
list.appendChild(li);
});
updateProgress(completed);
}
// Update progress bar and text
function updateProgress(completed) {
const total = habits.length;
const progressBar = document.getElementById("progressBar");
const progressText = document.getElementById("progressText");
if (total === 0) {
progressBar.style.width = "0%";
progressText.textContent = "0 habits completed";
document.getElementById("tipText").textContent = "";
return;
}
const percent = Math.round((completed / total) * 100);
progressBar.style.width = percent + "%";
progressText.textContent = `${completed} of ${total} habits completed (${percent}%)`;
}
// Initial render
renderHabits();