Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Module Pattern (IIFE)

Modern JavaScriptmedium

Course · Section 17: Modern JavaScript Development: Modules, Tooling, and Functional · Lecture 286: The Module Pattern

Implement `moduleCounter(times)` using the module pattern: an IIFE returns an object with `inc` and `get`, keeping the count private. Increment `times` times and return the count.

Sample tests

Input: moduleCounter(3)
Output: 3
Input: moduleCounter(0)
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Exposing count directly would break the encapsulation the module provides.
Visualize this concept: ES Modules →
Approach & explanation (try first)

The module pattern uses an IIFE closure to keep state private and expose only chosen methods.

Loading...
⌘/Ctrl + Enter

Run your code to see results.