Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Private Class Field

Object-Oriented Programminghard

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 236: Encapsulation: Private Class Fields and Methods

Using a class with a private `#count` field, an `inc()` method, and a `value` getter, implement `privateCounter(times)` returning the count after `inc()` is called `times` times.

Sample tests

Input: privateCounter(3)
Output: 3
Input: privateCounter(0)
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Accessing #count from outside the class is a syntax error.
Visualize this concept: Getters and Setters →
Approach & explanation (try first)

A private field encapsulates state, mutated only through the class's own methods.

Loading...
⌘/Ctrl + Enter

Run your code to see results.