Skip to main content

JavaScript Module Patterns — exercises and bug fixes

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

    Build a counter module

    easy10 min

    Keep a count private and expose an increment method.

  2. 2

    Hide private state

    easy10 min

    Show that the private count cannot be read directly.

  3. 3

    Use the revealing pattern

    easy10 min

    Declare two helpers privately and reveal only one.

  4. 4

    Namespace under one global

    easy10 min

    Attach a module to a single namespace object.

  5. 5

    Compare the pattern with a plain object

    medium10 min

    Show both approaches give the same result.

Find the bug(5)

  1. 1

    Fix the private value exposed

    easy8 min

    count should not appear on the returned object.

  2. 2

    Fix the IIFE never called

    easy8 min

    Without the trailing brackets, the function is never invoked.

  3. 3

    Fix the helper revealed by mistake

    easy8 min

    Only double was meant to be exposed.

  4. 4

    Fix the namespace overwritten

    easy8 min

    Reassigning MyApp loses whatever was already attached to it.

  5. 5

    Fix the two separately built objects

    easy8 min

    Two separate object literals are never equal, even with identical contents.

Ready to write the code?

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

Open the workspace