Impossible button

JavaScript
Medium

Objective

Create a button that attempts to avoid being clicked by moving away from the mouse cursor.

Requirements

  1. Create a button that's initially placed in a random position in the viewport

  2. Use JavaScript to move the button away from the mouse cursor when the cursor comes within a certain distance

  3. Add a counter that keeps track of how many times the user successfully clicks the button

  4. The button should not leave the viewport

Starting code

// Create your own HTML & CSS document.addEventListener("DOMContentLoaded", () => { const button = document.getElementById("impossibleButton"); const clickCounter = document.getElementById("clickCounter"); let counter = 0; // Initialise button position // TODO // Event Listeners // TODO button.addEventListener("click", () => { counter++; clickCounter.innerText = counter; }); });