Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
102 lines (81 loc) · 2.75 KB
/
Copy pathscript.js
File metadata and controls
102 lines (81 loc) · 2.75 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
let habits = [];
let habitStates = {}; // ✅ stores checkbox status per habit index
let count = localStorage.getItem("habitScore")
? parseInt(localStorage.getItem("habitScore"))
: 0;
updateScore();
// -------------------- Random Fruit Position --------------------
function getRandomPosition() {
return {
x: Math.floor(Math.random() * 260),
y: Math.floor(Math.random() * 260)
};
}
// -------------------- Score Update --------------------
function updateScore() {
const box = document.getElementById("scoreText");
localStorage.setItem("habitScore", count);
box.innerText = count;
}
// -------------------- Render Habits --------------------
function renderHabits() {
const list = document.getElementById("habitList");
list.innerHTML = "";
habits.forEach((habit, index) => {
const item = document.createElement("div");
item.className = "habit-item";
const label = document.createElement("label");
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
// ✅ Restore saved state
checkbox.checked = habitStates[index] === true;
checkbox.onclick = () => {
habitStates[index] = checkbox.checked; // save state
toggleFruit(index, checkbox.checked);
};
label.appendChild(checkbox);
label.appendChild(document.createTextNode(" " + habit));
item.appendChild(label);
list.appendChild(item);
});
}
// -------------------- Fruit Functions --------------------
function addFruit(index) {
// ❌ Prevent duplicate fruit
if (document.getElementById("fruit-" + index)) return;
const fruit = document.createElement("img");
fruit.src = "apple.webp";
fruit.className = "fruit";
fruit.id = "fruit-" + index;
const pos = getRandomPosition();
fruit.style.left = pos.x + "px";
fruit.style.top = pos.y + "px";
document.getElementById("fruitContainer").appendChild(fruit);
}
function removeFruit(index) {
const fruit = document.getElementById("fruit-" + index);
if (fruit) fruit.remove();
}
// -------------------- Toggle Fruit --------------------
function toggleFruit(index, checked) {
if (checked) addFruit(index);
else removeFruit(index);
}
// -------------------- Add Habit --------------------
document.getElementById("habitForm").onsubmit = (event) => {
event.preventDefault();
const name = document.getElementById("habitName").value.trim();
if (!name) return;
habits.push(name);
habitStates[habits.length - 1] = false; // new habit default unchecked
document.getElementById("habitName").value = "";
renderHabits();
};
// -------------------- Reset --------------------
document.getElementById("resetBtn").onclick = () => {
habits = [];
habitStates = {};
document.getElementById("fruitContainer").innerHTML = "";
renderHabits();
};
renderHabits();