-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
180 lines (157 loc) · 5.3 KB
/
Copy pathscript.js
File metadata and controls
180 lines (157 loc) · 5.3 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
const path = window.location.pathname;
// -------- INDEX PAGE --------
const isIndex =
path === "/" ||
path.endsWith("/index.html") ||
path.endsWith("/BMIBuddy/") ||
path.endsWith("/BMIBuddy");
if (isIndex) {
setTimeout(() => {
document.body.style.transition = "opacity 0.3s ease";
document.body.style.opacity = "0";
setTimeout(() => {
window.location.href = "selection.html"; // no ./ needed
}, 300);
}, 2000);
}
// -------- SELECTION PAGE --------
if (path.endsWith("selection.html")) {
let selectedGender = null;
window.selectGender = function (gender) {
selectedGender = gender;
document.querySelector(".gender-m").classList.remove("active");
document.querySelector(".gender-f").classList.remove("active");
if (gender === "male") {
document.querySelector(".gender-m").classList.add("active");
} else {
document.querySelector(".gender-f").classList.add("active");
}
};
window.updateValue = function (type, change) {
const input = document.getElementById(`${type}Value`);
let current = parseInt(input.value);
if (isNaN(current)) current = 1;
const newValue = Math.max(1, current + change);
input.value = newValue;
};
window.goToResult = function () {
const feet = parseInt(document.getElementById("heightFeet").value);
const inches = parseInt(document.getElementById("heightInches").value);
const weightLbs = parseInt(document.getElementById("weightValue").value);
const age = parseInt(document.getElementById("ageValue").value);
if (!selectedGender) {
alert("Please select a gender.");
return;
}
if (isNaN(feet) || isNaN(inches)) {
alert("Please enter your height.");
return;
}
const totalInches = feet * 12 + inches;
const heightMeters = totalInches * 0.0254;
const weightKg = weightLbs * 0.453592;
const bmi = weightKg / (heightMeters * heightMeters);
window.location.href = `result.html?bmi=${bmi.toFixed(1)}`;
};
}
// -------- RESULT PAGE --------
if (path.endsWith("result.html")) {
const heading = document.getElementById("heading-click");
const hourglass = document.getElementById("hourglass-click");
if (heading) {
heading.addEventListener("click", () => {
window.location.href = "selection.html";
});
}
if (hourglass) {
hourglass.addEventListener("click", () => {
window.location.href = "selection.html";
});
}
const params = new URLSearchParams(window.location.search);
const bmi = parseFloat(params.get("bmi"));
const bmiValueElement = document.getElementById("bmi-value");
const bmiCategoryElement = document.getElementById("bmi-category");
const bmiRangeElement = document.getElementById("bmi-range");
const recommendationTitle = document.getElementById("recommendation-title");
const caloriesList = document.getElementById("calories-list");
const nutrientsList = document.getElementById("nutrients-list");
const recommendations = {
underweight: {
title: "Underweight",
color: "#B2FF66",
calories: [
"Consume more high-calorie foods like nuts, avocados, and healthy oils",
"Increase portion sizes during meals",
],
nutrients: [
"Focus on foods rich in protein (lean meats, fish, eggs, legumes)",
"Include complex carbohydrates (whole grains, sweet potatoes)",
"Eat plenty of fruits and vegetables",
],
},
normal: {
title: "Normal",
color: "#00FF99",
calories: [
"Continue balanced diet with whole foods",
"Maintain regular physical activity",
],
nutrients: [
"Eat a variety of fruits, vegetables, lean proteins, and whole grains",
"Limit sugary snacks and processed foods",
],
},
overweight: {
title: "Overweight",
color: "#FFA500",
calories: [
"Reduce high-calorie snacks and processed foods",
"Control portion sizes",
],
nutrients: [
"Eat more fiber-rich foods and lean proteins",
"Choose low-calorie cooking methods (steaming, grilling)",
],
},
obese: {
title: "Obese",
color: "#FF4C4C",
calories: [
"Work with a dietitian to build a calorie deficit plan",
"Avoid sugary drinks and fast food",
],
nutrients: [
"Prioritize high-fiber vegetables, lean proteins, and healthy fats",
"Exercise regularly with low-impact routines (walking, swimming)",
],
},
};
function getBMICategory(bmi) {
if (bmi < 18.5) return "underweight";
if (bmi < 25) return "normal";
if (bmi < 30) return "overweight";
return "obese";
}
function renderResult() {
if (isNaN(bmi)) {
bmiValueElement.textContent = "--";
bmiCategoryElement.textContent = "Unknown";
return;
}
const category = getBMICategory(bmi);
const data = recommendations[category];
bmiValueElement.textContent = bmi.toFixed(1);
bmiCategoryElement.textContent = data.title;
bmiCategoryElement.style.color = data.color;
bmiRangeElement.value = bmi;
recommendationTitle.textContent = data.title;
caloriesList.innerHTML = data.calories
.map((item) => `<li>${item}</li>`)
.join("");
nutrientsList.innerHTML = data.nutrients
.map((item) => `<li>${item}</li>`)
.join("");
}
renderResult();
}