@@ -129,26 +129,21 @@ <h2>Results</h2>
129
129
let totalCO2Reduction = 0 ;
130
130
let totalPrice = 0 ;
131
131
132
- // Tree 1
133
- const tree1Quantity = parseInt ( document . getElementById ( 'tree1Output' ) . textContent . split ( " " ) [ 0 ] ) || 0 ;
134
- const tree1CO2Absorption = 10 ; // replace with actual CO2 absorption rate
135
- const tree1Price = 5 ; // replace with actual price
136
- totalCO2Reduction += tree1Quantity * tree1CO2Absorption ;
137
- totalPrice += tree1Quantity * tree1Price ;
138
-
139
- // Tree 2
140
- const tree2Quantity = parseInt ( document . getElementById ( 'tree2Output' ) . textContent . split ( " " ) [ 0 ] ) || 0 ;
141
- const tree2CO2Absorption = 15 ; // replace with actual CO2 absorption rate
142
- const tree2Price = 8 ; // replace with actual price
143
- totalCO2Reduction += tree2Quantity * tree2CO2Absorption ;
144
- totalPrice += tree2Quantity * tree2Price ;
145
-
146
- // Tree 3
147
- const tree3Quantity = parseInt ( document . getElementById ( 'tree3Output' ) . textContent . split ( " " ) [ 0 ] ) || 0 ;
148
- const tree3CO2Absorption = 20 ; // replace with actual CO2 absorption rate
149
- const tree3Price = 10 ; // replace with actual price
150
- totalCO2Reduction += tree3Quantity * tree3CO2Absorption ;
151
- totalPrice += tree3Quantity * tree3Price ;
132
+ // Loop through each tree type
133
+ for ( let i = 1 ; i <= 3 ; i ++ ) {
134
+ const treeId = 'tree' + i ;
135
+
136
+ // Get tree quantity
137
+ const treeQuantity = parseInt ( document . getElementById ( treeId + 'Output' ) . textContent . split ( " " ) [ 0 ] ) || 0 ;
138
+
139
+ // Get CO2 absorption rate and price from getPricePerTree function
140
+ const treeCO2Absorption = i * 5 ; // Replace with actual CO2 absorption rate
141
+ const treePrice = getPricePerTree ( treeId ) ;
142
+
143
+ // Update total CO2 reduction and price
144
+ totalCO2Reduction += treeQuantity * treeCO2Absorption ;
145
+ totalPrice += treeQuantity * treePrice ;
146
+ }
152
147
153
148
// Update results
154
149
const resultCO2 = document . getElementById ( 'resultCO2' ) ;
0 commit comments