Report which key was pressed.
Acceptance criteria
- 1.#out should show a.
JavaScript Tutorial · JavaScript Events
These 10 tasks go with the JavaScript Keyboard Events chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Keyboard events report which key was pressed and let you respond to it.” .
1.Report the key pressedexercise · easy · 10 min
Report which key was pressed.
Passes when: #out should show a.
2.React to Enterexercise · easy · 10 min · practice plan
Report Submitted only when Enter is pressed.
Passes when: #out should show Submitted.
3.Detect a shortcutexercise · easy · 10 min · practice plan
Report Saved when Ctrl and s are pressed together.
Passes when: #out should show Saved.
4.Use the input eventexercise · easy · 10 min · practice plan
Report the field value after it changes.
Passes when: #out should show abc.
5.Listen on the whole pageexercise · medium · 10 min · practice plan
Report Closed when Escape is pressed anywhere.
Passes when: #out should show Closed.
6.Fix keyCode used instead of keybug fix · easy · 8 min
event.key gives the key name directly.
Passes when: #out should show Enter.
7.Fix the lowercase key namebug fix · easy · 8 min · practice plan
Named keys are capitalised, so enter never matches.
Passes when: #out should show Submitted.
8.Fix keydown used to read the new valuebug fix · easy · 8 min · practice plan
On keydown the value has not been updated yet.
Passes when: #out should show abc.
9.Fix the modifier never checkedbug fix · easy · 8 min · practice plan
The shortcut fires on s alone, without Ctrl.
Passes when: #out should show Saved.
10.Fix keypress used for Escapebug fix · easy · 8 min · practice plan
keypress is deprecated and never fires for Escape.
Passes when: #out should show Closed.
Between them these tasks cover keydown, event.key and Modifier keys.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.