Skip to main content

JavaScript Modules — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Modules 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

    Simulate a private scope

    easy10 min

    Keep a value private and expose only a method that uses it.

  2. 2

    Hide an unexported name

    easy10 min

    Show that a private helper cannot be reached from outside.

  3. 3

    Run a module once

    easy10 min

    Show that importing the same module twice shares one result.

  4. 4

    Keep two modules separate

    easy10 min

    Show that two simulated modules do not share their private state.

  5. 5

    Show modules run in strict mode

    medium10 min

    Catch the error an undeclared assignment throws in strict mode.

Find the bug(5)

  1. 1

    Fix the private value exposed

    easy8 min

    The internal value should not be part of the returned object.

  2. 2

    Fix the shared global variable

    easy8 min

    Two plain scripts sharing one scope overwrite each other's variable.

  3. 3

    Fix the modules sharing state

    easy8 min

    Each module should keep its own private total instead of a variable both can see.

  4. 4

    Fix the module re-run unnecessarily

    easy8 min

    The same loaded module should be reused, not rebuilt.

  5. 5

    Fix strict mode never applied

    easy8 min

    Without strict mode, an undeclared assignment silently creates a global instead of throwing.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace