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
Simulate a private scope
easy10 minKeep a value private and expose only a method that uses it.
- 2
Hide an unexported name
easy10 minShow that a private helper cannot be reached from outside.
- 3
Run a module once
easy10 minShow that importing the same module twice shares one result.
- 4
Keep two modules separate
easy10 minShow that two simulated modules do not share their private state.
- 5
Show modules run in strict mode
medium10 minCatch the error an undeclared assignment throws in strict mode.
Find the bug(5)
- 1
Fix the private value exposed
easy8 minThe internal value should not be part of the returned object.
- 2
Fix the shared global variable
easy8 minTwo plain scripts sharing one scope overwrite each other's variable.
- 3
Fix the modules sharing state
easy8 minEach module should keep its own private total instead of a variable both can see.
- 4
Fix the module re-run unnecessarily
easy8 minThe same loaded module should be reused, not rebuilt.
- 5
Fix strict mode never applied
easy8 minWithout 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