|  | 
|  | 1 | +const game = { timer: 0, start: null }; | 
|  | 2 | + | 
|  | 3 | +// Create Message Element | 
|  | 4 | +const message = document.createElement("div"); | 
|  | 5 | +message.classList.add("message"); | 
|  | 6 | +message.textContent = "Press To Start"; | 
|  | 7 | +document.body.prepend(message); | 
|  | 8 | + | 
|  | 9 | +// Create a Box | 
|  | 10 | +const box = document.createElement("div"); | 
|  | 11 | +box.classList.add("box"); | 
|  | 12 | + | 
|  | 13 | +const output = document.querySelector(".output"); | 
|  | 14 | +output.append(box); | 
|  | 15 | + | 
|  | 16 | +box.addEventListener("click", () => { | 
|  | 17 | +  box.textContent = ""; | 
|  | 18 | +  box.style.display = "none"; | 
|  | 19 | +  game.timer = setTimeout(addBox, randomNumbers(3000)); | 
|  | 20 | +  if (!game.start) { | 
|  | 21 | +    message.textContent = "Watch for element and click it"; | 
|  | 22 | +  } else { | 
|  | 23 | +    const current = new Date().getTime(); | 
|  | 24 | +    const duration = (current - game.start) / 1000; | 
|  | 25 | +    message.textContent = `It took ${duration} seconds to click`; | 
|  | 26 | +  } | 
|  | 27 | +}); | 
|  | 28 | + | 
|  | 29 | +function randomNumbers(max) { | 
|  | 30 | +  return Math.floor(Math.random() * max); | 
|  | 31 | +} | 
|  | 32 | + | 
|  | 33 | +function addBox() { | 
|  | 34 | +  game.start = new Date().getTime(); | 
|  | 35 | +  const container = output.getBoundingClientRect(); | 
|  | 36 | +  const dim = [randomNumbers(50) + 20, randomNumbers(50) + 20]; | 
|  | 37 | +  box.style.display = "block"; | 
|  | 38 | +  box.style.width = `${dim[0]}px`; | 
|  | 39 | +  box.style.height = `${dim[1]}px`; | 
|  | 40 | +  box.style.backgroundColor = "#" + Math.random().toString(16).substr(-6); | 
|  | 41 | +  box.style.left = randomNumbers(container.width - dim[0]) + "px"; | 
|  | 42 | +  box.style.top = randomNumbers(container.height - dim[1]) + "px"; | 
|  | 43 | +  box.style.borderRadius = randomNumbers(50) + "%"; | 
|  | 44 | +} | 
0 commit comments