JavaScript Keyboard Events — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Keyboard Events 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
Report the key pressed
easy10 minReport which key was pressed.
- 2
React to Enter
easy10 minReport Submitted only when Enter is pressed.
- 3
Detect a shortcut
easy10 minReport Saved when Ctrl and s are pressed together.
- 4
Use the input event
easy10 minReport the field value after it changes.
- 5
Listen on the whole page
medium10 minReport Closed when Escape is pressed anywhere.
Find the bug(5)
- 1
Fix keyCode used instead of key
easy8 minevent.key gives the key name directly.
- 2
Fix the lowercase key name
easy8 minNamed keys are capitalised, so enter never matches.
- 3
Fix keydown used to read the new value
easy8 minOn keydown the value has not been updated yet.
- 4
Fix the modifier never checked
easy8 minThe shortcut fires on s alone, without Ctrl.
- 5
Fix keypress used for Escape
easy8 minkeypress is deprecated and never fires for Escape.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace