Keep a value private and expose only a method that uses it.
Acceptance criteria
- 1.#out should show 10.
JavaScript Tutorial · JavaScript Modules
These 10 tasks go with the JavaScript Modules 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: “A module is a file that can export values for other files to import.” .
1.Simulate a private scopeexercise · easy · 10 min
Keep a value private and expose only a method that uses it.
Passes when: #out should show 10.
2.Hide an unexported nameexercise · easy · 10 min · practice plan
Show that a private helper cannot be reached from outside.
Passes when: #out should show hidden.
3.Run a module onceexercise · easy · 10 min · practice plan
Show that importing the same module twice shares one result.
Passes when: #out should show same object.
4.Keep two modules separateexercise · easy · 10 min · practice plan
Show that two simulated modules do not share their private state.
Passes when: #out should show 5 and 10.
5.Show modules run in strict modeexercise · medium · 10 min · practice plan
Catch the error an undeclared assignment throws in strict mode.
Passes when: #out should show ReferenceError.
6.Fix the private value exposedbug fix · easy · 8 min
The internal value should not be part of the returned object.
Passes when: #out should show hidden.
7.Fix the shared global variablebug fix · easy · 8 min · practice plan
Two plain scripts sharing one scope overwrite each other's variable.
Passes when: #out should show 5.
8.Fix the modules sharing statebug fix · easy · 8 min · practice plan
Each module should keep its own private total instead of a variable both can see.
Passes when: #out should show 5 and 10.
9.Fix the module re-run unnecessarilybug fix · easy · 8 min · practice plan
The same loaded module should be reused, not rebuilt.
Passes when: #out should show 1.
10.Fix strict mode never appliedbug fix · easy · 8 min · practice plan
Without strict mode, an undeclared assignment silently creates a global instead of throwing.
Passes when: #out should show ReferenceError.
Between them these tasks cover Module scope, Isolation and Caching.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.