diff --git a/js-features/cookie-clicker/task.js b/js-features/cookie-clicker/task.js index e69de29bb2..5bfa9c8bd2 100644 --- a/js-features/cookie-clicker/task.js +++ b/js-features/cookie-clicker/task.js @@ -0,0 +1,36 @@ +document.addEventListener('DOMContentLoaded', () => { + const cookie = document.getElementById('cookie'); + + const counter = new Counter(); + + cookie.addEventListener('click', () => { + counter.plusClick(); + }); +}); + +class Counter { + constructor() { + this.clicksCounter = document.getElementById('clicker__counter'); + this.countSpeedElement = document.getElementById('clicker__avg'); + this.clicksCount = Number(this.clicksCounter.innerText); + this.prevClickTime = null; + this.currentClickTime = null; + } + + plusClick() { + this.currentClickTime = new Date(); + this.clicksCount = this.clicksCount + 1; + this.clicksCounter.innerText = this.clicksCount; + this.updateClickAvg(); + this.prevClickTime = this.currentClickTime; + } + + updateClickAvg() { + if (!this.prevClickTime) { + return; + } + + const diff = (this.currentClickTime.getTime() - this.prevClickTime.getTime()) / 1000; + this.countSpeedElement.innerText = (1 / diff).toFixed(2); + } +}