Код: Выделить всё
class GreenHospitality {
constructor() {
this.co2Saved = 0;
this.treeThreshold = 100.00; // Threshold to plant a tree
}
updateCO2() {
setTimeout(() => {
const newCO2 = Math.random() * 5 + 0.01; // Small random CO2 increase
this.co2Saved += newCO2;
console.log(this.co2Saved.toFixed(2));
if (this.co2Saved >= this.treeThreshold) {
console.log("You can plant a tree!");
}
}, 3000);
}
}
< /code>
Это для включения кнопки: < /p>
Plant a Tree
Upgrade Tree
if (this.co2Saved >= this.treeThreshold) {
this.treeStatus.textContent = "You can now plant a tree!";
this.plantTreeBtn.disabled = false;
} else {
this.treeStatus.textContent = "You need more CO₂ to plant a tree!";
this.plantTreeBtn.disabled = true;
}
< /code>
Слушатели событий кнопок: < /p>
document.getElementById("plant-tree-btn").addEventListener("click", () => game.plantTree());
document.getElementById("upgrade-tree-btn").addEventListener("click", () => game.upgradeTree());
[*] с использованием math.floor () и math.ceil () , чтобы настроить значение
Подробнее здесь: https://stackoverflow.com/questions/795 ... -the-limit