Keep a count private and expose an increment method.
Acceptance criteria
- 1.#out should show 1 and 2.
JavaScript Tutorial · JavaScript Modules
These 10 tasks go with the JavaScript Module Patterns 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: “Before real modules existed, JavaScript used closures to fake the same isolation.” .
1.Build a counter moduleexercise · easy · 10 min
Keep a count private and expose an increment method.
Passes when: #out should show 1 and 2.
2.Hide private stateexercise · easy · 10 min · practice plan
Show that the private count cannot be read directly.
Passes when: #out should show hidden.
3.Use the revealing patternexercise · easy · 10 min · practice plan
Declare two helpers privately and reveal only one.
Passes when: #out should show 10.
4.Namespace under one globalexercise · easy · 10 min · practice plan
Attach a module to a single namespace object.
Passes when: #out should show 8.
5.Compare the pattern with a plain objectexercise · medium · 10 min · practice plan
Show both approaches give the same result.
Passes when: #out should show same result.
6.Fix the private value exposedbug fix · easy · 8 min
count should not appear on the returned object.
Passes when: #out should show hidden.
7.Fix the IIFE never calledbug fix · easy · 8 min · practice plan
Without the trailing brackets, the function is never invoked.
Passes when: #out should show 1.
8.Fix the helper revealed by mistakebug fix · easy · 8 min · practice plan
Only double was meant to be exposed.
Passes when: #out should show hidden.
9.Fix the namespace overwrittenbug fix · easy · 8 min · practice plan
Reassigning MyApp loses whatever was already attached to it.
Passes when: #out should show still there.
10.Fix the two separately built objectsbug fix · easy · 8 min · practice plan
Two separate object literals are never equal, even with identical contents.
Passes when: #out should show same object.
Between them these tasks cover IIFE, Revealing module pattern and Private state.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.