JavaScript addEventListener — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript addEventListener lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Run a listener once
easy10 minUse the once option so the count never goes past 1.
- 2
Attach to every button
easy10 minLoop over the buttons so any of them increases the count.
- 3
Share one handler
easy10 minAttach the same named function to both buttons.
- 4
Add two listeners
easy10 minAttach two listeners so the count reaches 2 on one click.
- 5
Listen for a double click
medium10 minReport Double when the button is double clicked.
Find the bug(5)
- 1
Fix the missing once option
easy8 minThe listener should stop after the first click.
- 2
Fix the listener attached to a list
easy8 minA NodeList has no addEventListener; each element needs its own.
- 3
Fix only the first button handled
easy8 minquerySelector attaches to one button, so the second does nothing.
- 4
Fix the duplicate named handler
easy8 minAdding the same named function twice only counts once.
- 5
Fix once used where it should not be
easy8 minThe count should keep rising on every click.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace