-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresults.html
233 lines (196 loc) · 8.48 KB
/
results.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="resultsStyling.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Mulish:ital,wght@0,200;0,600;1,200&family=Righteous&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Titillium+Web:wght@300&display=swap" rel="stylesheet">
<style>
table {
border-collapse: collapse;
width: 300px;
margin: 0 auto;
}
tr {
border-bottom: 1px solid #ddd;
}
td {
padding: 8px;
text-align: left;
font-weight: bold;
background-color: #f2f2f2;
}
#total-emissions {
background-color: #333;
color: white;
font-size: 18px;
}
/* Style individual rows if needed */
/* For example, to highlight the "Your Car Emissions" row */
/* #car-emissions {
background-color: #f0c419;
} */
</style>
</head>
<body>
<header>
<div class="logo">
<img src="https://drive.google.com/uc?id=1JiC096YTg1DDxHs0sZYPd6TiGmEE-ZM9" alt="Rotating globe with footprints" style="width: 75px; border-radius: 50%;">
</div>
<div id = "title-slogan">
<p>Results! </p>
</div>
</header>
<div id="main-body">
<table>
<tr>
<td id="total-emissions">Your Total Emissions:</td>
<td id="total-emissions">0</td>
</tr>
<tr>
<td id="foods-co2">Your Food Emissions:</td>
<td>0</td>
</tr>
<tr>
<td id="car-emissions">Your Car Emissions:</td>
<td>0</td>
</tr>
<tr>
<td id="housing-emmisions">Your Housing Emissions:</td>
<td>0</td>
</tr>
<tr>
<td id="water-emissions">Your Water Emissions:</td>
<td>0</td>
</tr>
</table>
</div>
<footer>
<p>© Duke University - MLH HackDuke 2023</p>
<p>Developed by: Alex Baptiste, Joseph Ricci, Michael Puglise, & Riley Harper</p>
</footer>
<script>
//npm install csv-parse
// JavaScript code to set a variable
var totalEmissions;
var foodsCo2;
var carEmissions;
var housingEmissions;
var waterEmissions;
//--------------------------------
// Constants
const gCo2PerBottle = 0.1825; // lbs of CO2
const lbsOfFoodConsumedPerYear = 2000;
// Mock data (replace with actual data loading if needed)
const surveyResultsData = [
/* ... */ // Replace with your survey data
];
const stateCo2Data = [
/* ... */ // Replace with state CO2 data
];
const vehicleEmissionsData = [
/* ... */ // Replace with vehicle emissions data
];
const co2ByElectricalGridData = [
/* ... */ // Replace with CO2 by electrical grid data
];
const co2ByFoodData = [
/* ... */ // Replace with CO2 by food data
];
// Function to calculate food emissions
function calculateFoodEmissions(foods) {
let foodsCo2 = 0;
for (const food of foods) {
try {
const foodCo2 = co2ByFoodData.find(
(item) => item["Food product"] === food
);
if (foodCo2) {
foodsCo2 += foodCo2["Total_emissions"];
}
} catch (error) {
console.error("Error calculating food emissions: " + error);
}
}
return Math.round(foodsCo2 * lbsOfFoodConsumedPerYear);
}
// Function to calculate car emissions
function calculateCarEmissions(carYear, milesPerYear) {
// Define mappings for car years (replace with actual data)
const carYearMappings = {
/* Add mappings for car years */
};
// Define default miles per year if not in provided ranges
if (milesPerYear === "0-1000") {
milesPerYear = 500;
} else if (milesPerYear === "1000-15,000") {
milesPerYear = 8000;
} else if (milesPerYear === "I do not drive") {
milesPerYear = 0;
} else {
milesPerYear = 20000;
}
const carCh4 = milesPerYear * carYearMappings[carYear].ch4;
const carN2o = milesPerYear * carYearMappings[carYear].n2o;
return Math.round(carCh4 + carN2o);
}
// Function to calculate housing emissions
function calculateHousingEmissions(housing) {
// Define mappings for housing types (replace with actual data)
const housingMappings = {
/* Add mappings for housing types */
};
const userState = surveyResultsData[surveyResultsData.length - 1]["What state are you from?"];
const userStateEmissions = stateCo2Data.find(
(item) => item.state === userState
)?.avg_co2;
const userStateElectricity = co2ByElectricalGridData.find(
(item) => item.state === userState
)?.["lb/mwh"];
if (!userStateEmissions || !userStateElectricity) {
console.error("Error retrieving state emissions data.");
return 0;
}
const housingElectricityUsage = housingMappings[housing];
return Math.round(userStateElectricity * housingElectricityUsage);
}
// Function to calculate water emissions
function calculateWaterEmissions(water) {
if (water === "Refillable Bottle") {
return 0;
} else {
return Math.round(156 * gCo2PerBottle); // 156 bottles per year
}
}
// Calculate emissions
const numResults = surveyResultsData.length;
const foods = surveyResultsData[numResults - 1]["What food products make up your diet?"].split(", ");
const carYear = surveyResultsData[numResults - 1]["If you drive what year was your car made?"];
const milesPerYear = surveyResultsData[numResults - 1]["If you drive how many miles a year on average?"];
const housing = surveyResultsData[numResults - 1]["What is your housing situation?"];
const water = surveyResultsData[numResults - 1]["Do you mostly drink from a refillable Water Bottle or drink bottled water?"];
foodsCo2 = calculateFoodEmissions(foods);
carEmissions = calculateCarEmissions(carYear, milesPerYear);
housingEmissions = calculateHousingEmissions(housing);
waterEmissions = calculateWaterEmissions(water);
totalEmissions = (foodsCo2 + carEmissions + housingEmissions + waterEmissions);
//--------------------------------
// Access and display the variable in HTML
var resultElement1 = document.getElementById("total-emissions");
resultElement1.innerHTML = totalEmissions;
var resultElement2 = document.getElementById("foods-co2");
resultElement2.innerHTML = foodsCo2;
var resultElement3 = document.getElementById("car-emissions");
resultElement3.innerHTML = carEmissions;
var resultElement4 = document.getElementById("housing-emmisions");
resultElement4.innerHTML = housingEmissions;
var resultElement5 = document.getElementById("water-emissions");
resultElement5.innerHTML = waterEmissions;
</script>
<!--src="stepForward.js"-->
</body>
</html>