Skip to main content

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. 1

    Report the key pressed

    easy10 min

    Report which key was pressed.

  2. 2

    React to Enter

    easy10 min

    Report Submitted only when Enter is pressed.

  3. 3

    Detect a shortcut

    easy10 min

    Report Saved when Ctrl and s are pressed together.

  4. 4

    Use the input event

    easy10 min

    Report the field value after it changes.

  5. 5

    Listen on the whole page

    medium10 min

    Report Closed when Escape is pressed anywhere.

Find the bug(5)

  1. 1

    Fix keyCode used instead of key

    easy8 min

    event.key gives the key name directly.

  2. 2

    Fix the lowercase key name

    easy8 min

    Named keys are capitalised, so enter never matches.

  3. 3

    Fix keydown used to read the new value

    easy8 min

    On keydown the value has not been updated yet.

  4. 4

    Fix the modifier never checked

    easy8 min

    The shortcut fires on s alone, without Ctrl.

  5. 5

    Fix keypress used for Escape

    easy8 min

    keypress 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