-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnutrition.html
240 lines (204 loc) · 7.58 KB
/
nutrition.html
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nutrition Score Calculator</title>
<style>
body {
font-family: 'Arial', sans-serif;
background: url('bg.gif') center/cover no-repeat;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
height: 100vh;
background-size: 100% 100%;
}
#nutritionForm {
background-color: #fff;
border-radius: 8px;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 400px;
width: 100%;
box-sizing: border-box;
text-align: center;
margin-bottom: 20px;
position: relative;
}
h2 {
color: #3498db;
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
label {
margin-top: 10px;
font-size: 14px;
}
input {
width: calc(100% - 20px);
padding: 10px;
margin-top: 5px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
}
button {
background-color: #fff;
color: #3498db;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 10px;
font-size: 16px;
}
#loader {
display: none;
border: 4px solid #f3f3f3;
border-radius: 50%;
border-top: 4px solid #3498db;
width: 20px;
height: 20px;
animation: spin 1s linear infinite;
position: absolute;
bottom: -50 px;
left: 50%;
transform: translateX(-50%);
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
#nutritionList {
list-style: none;
padding: 0;
max-width: 400px;
margin: 20px auto;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
padding: 10px;
box-sizing: border-box;
text-align: left;
}
#totalScoreContainer {
background-color: #fff;
border-radius: 100px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 460px;
width: 100%;
box-sizing: border-box;
text-align: center;
padding: 20px;
margin: 20px;
position: relative;
}
#totalScore {
font-size: 18px;
margin-top: 0px;
color: #27ae60;
}
</style>
</head>
<body>
<div id="nutritionForm">
<h2>Nutrition Score Calculator</h2>
<form id="foodForm">
<label for="foodName">Food Name:</label>
<input type="text" id="foodName" required>
<br><br>
<label for="servings">Servings (1 serving = 100g):</label>
<input type="text" id="servings" required>
<button type="button" onclick="addFood()">Add Food</button>
<button type="button" onclick="manualReset()">Manual Reset</button>
</form>
<div id="loader"></div>
</div>
<ul id="nutritionList"></ul>
<div id="totalScoreContainer">
<div id="totalScore">Total Nutrition Score: <span id="score">0</span></div>
</div>
<script>
let totalScore = parseFloat(localStorage.getItem('totalScore')) || 0;
// Update total score on page load
updateTotalScore();
async function addFood() {
const foodName = document.getElementById('foodName').value;
const servings = parseFloat(document.getElementById('servings').value);
const apiKey = 'zCoMaVeKCgl2nVfbe6tG469MYVBtE1YE5uTgKAkb';
const apiUrl = `https://api.nal.usda.gov/fdc/v1/foods/search?api_key=${apiKey}&query=${foodName}`;
// Show loader while fetching data
document.getElementById('loader').style.display = 'block';
try {
const response = await fetch(apiUrl);
const data = await response.json();
// Hide loader after data is fetched
document.getElementById('loader').style.display = 'none';
if (response.ok && data.foods && data.foods.length > 0) {
const food = data.foods[0];
const nutrients = {
calories: food.foodNutrients.find(nutrient => nutrient.nutrientName === 'Energy').value,
protein: food.foodNutrients.find(nutrient => nutrient.nutrientName === 'Protein').value,
carbs: food.foodNutrients.find(nutrient => nutrient.nutrientName === 'Carbohydrate, by difference').value,
fat: food.foodNutrients.find(nutrient => nutrient.nutrientName === 'Total lipid (fat)').value,
};
const foodScore = calculateScore(nutrients, servings);
totalScore += foodScore;
updateUI(foodName, foodScore);
updateTotalScore();
// Save totalScore to local storage
localStorage.setItem('totalScore', totalScore.toString());
} else {
console.error(`Error: ${response.status} - ${data.message}`);
}
} catch (error) {
console.error('Error fetching data:', error);
}
}
function calculateScore(nutrients, servings) {
return servings * (nutrients.calories + nutrients.protein + nutrients.carbs + nutrients.fat);
}
function updateUI(foodName, foodScore) {
const nutritionList = document.getElementById('nutritionList');
const newItem = document.createElement('li');
newItem.textContent = `${foodName}: ${foodScore.toFixed(2)} points`;
nutritionList.appendChild(newItem);
// Remove the item after 3 seconds
setTimeout(() => {
newItem.remove();
}, 3000);
}
function updateTotalScore() {
const totalScoreElement = document.getElementById('score');
totalScoreElement.textContent = totalScore.toFixed(2);
}
// Reset totalScore at 12 am
setInterval(() => {
const now = new Date();
if (now.getHours() === 0 && now.getMinutes() === 0) {
totalScore = 0;
updateTotalScore();
localStorage.setItem('totalScore', totalScore.toString());
}
}, 60000); // Check every minute
// Manual reset function
function manualReset() {
totalScore = 0;
updateTotalScore();
localStorage.setItem('totalScore', totalScore.toString());
}
</script>
</body>
</html>