-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarbon.mjs
33 lines (32 loc) · 1.65 KB
/
carbon.mjs
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
export function calculateEmission(milesTraveled, mode) {
let co2EmissionsKg;
milesTraveled = parseInt(milesTraveled.replace(" mi", ""), 10);
// console.log(milesTraveled,mode);
if (mode === 'DRIVING') {
const avgFuelEfficiencyMpg = 25;
const milesToKilometers = 1.60934;
const emissionFactorGasoline = 2.31;
const fuelEfficiencyLPer100km = 235.2 / avgFuelEfficiencyMpg;
const fuelUsedLiters = (milesTraveled * milesToKilometers) / (fuelEfficiencyLPer100km / 100);
co2EmissionsKg = fuelUsedLiters * emissionFactorGasoline;
} else if (mode === 'TRANSIT') {
const milesToKilometers = 1.60934;
const gallonsToLiters = 3.78541;
const emissionFactorDiesel = 2.68;
const fuelEfficiencyLPer100km = 235.2 / 6; // Assuming fuel efficiency is 6 gallons per 100 miles for transit
const fuelUsedLiters = (milesTraveled * milesToKilometers) / (fuelEfficiencyLPer100km / 100);
co2EmissionsKg = fuelUsedLiters * emissionFactorDiesel;
} else if (mode === 'WALKING') {
const avgCaloriesPerMile = 75;
const carbonFootprintPer1000Calories = 3;
const totalCaloriesBurned = milesTraveled * avgCaloriesPerMile;
co2EmissionsKg = (totalCaloriesBurned / 1000) * carbonFootprintPer1000Calories;
} else { // assuming the mode is 'biking'
const avgCaloriesPerMile = 45;
const carbonFootprintPer1000Calories = 3;
const totalCaloriesBurned = milesTraveled * avgCaloriesPerMile;
co2EmissionsKg = (totalCaloriesBurned / 1000) * carbonFootprintPer1000Calories;
}
// console.log(co2EmissionsKg)
return Math.round(co2EmissionsKg);
}