1+
2+ const result = document . querySelector ( '.result' ) ;
3+ const form = document . querySelector ( 'form' ) ;
4+ const newButton = document . querySelector ( '#btn' ) ;
5+ const answerBox = document . querySelector ( '.answer-box' ) ;
6+
7+ console . log ( form )
8+ form . addEventListener ( 'submit' , ( e ) => {
9+ e . preventDefault ( ) ;
10+ let { bill, vehicle, mileage, distance, ac} = e . target ;
11+
12+ let gas = e . target . gas ;
13+
14+ let answer = calculateFootPrint ( bill . value , vehicle . value , mileage . value , distance . value , ac . value , ( gas ) ? gas . value : null ) ;
15+
16+ bill . value = '' ;
17+ vehicle . value = '' ;
18+ mileage . value = '' ;
19+ distance . value = '' ;
20+ ac . value = '' ;
21+ if ( gas ) {
22+ gas . value = '' ;
23+ }
24+ answerBox . style . display = 'flex' ;
25+ form . style . display = 'none' ;
26+ result . innerHTML = "You carbon emission is: " + answer . toFixed ( 4 ) ;
27+ } )
28+
29+
30+ newButton . addEventListener ( 'click' , ( ) => {
31+ // answerBox.style.display = 'none';
32+ location . reload ( ) ;
33+ // form.style.display = 'block';
34+ } )
35+
36+ function calculateFootPrint ( bill , vehicle , mileage , distance , ac , gas ) {
37+ let emission = 0 ;
38+
39+ if ( vehicle == 'petrol' ) {
40+ emissions_factor = 2.31 ;
41+ } else if ( vehicle == 'diesel' ) {
42+ emissions_factor = 2.68 ;
43+ }
44+
45+ emission += ( distance / mileage ) * emissions_factor
46+ console . log ( emission ) ;
47+ emissions_factor2 = 0.93
48+ emission = emission + bill * emissions_factor2
49+ console . log ( emission ) ;
50+
51+ if ( gas ) {
52+ emissions_factor3 = 2.983
53+ emission = emission + emissions_factor3 * gas
54+ }
55+
56+ return emission ;
57+ }
58+
59+
60+ // emission = 0
61+ // current = float(request.POST['electricity']) # In units = KWH
62+ // gas = float(request.POST['gas']) # In grams
63+ // vehicle = request.POST['vehicle'] # Car , Bus
64+ // milage = float(request.POST['mileage'])
65+ // distance =float( request.POST['distance'])
66+ // emissions_factor = 0
67+
68+ // if (vehicle == "Car"):
69+ // car_fuel = request.POST['fuel'] # Data is shared in the group
70+ // if car_fuel == 'gasoline':
71+ // emissions_factor = 2.31 # in kg CO2 per liter
72+ // elif car_fuel == 'diesel':
73+ // emissions_factor = 2.68 # in kg CO2 per liter
74+ // elif (vehicle == "Bus"):
75+ // car_fuel = request.POST['fuel'] # Data is shared in the group
76+ // emissions_factor = 0.68 # in kg CO2 per km
77+
78+ // # Fuel
79+
80+ // if vehicle == 'car':
81+ // emission = (distance / milage) * emissions_factor
82+ // elif vehicle == 'bus':
83+ // emission = distance * emissions_factor
84+ // # Electricity
85+ // emissions_factor = 0.93
86+ // emission = emission + current*emissions_factor
87+
88+ // #LPG
89+ // emissions_factor = 2.983
90+ // emission = emission + emissions_factor*gas
0 commit comments