Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Counter Class

Object-Oriented Programmingeasy

Course · Section 14: Object-Oriented Programming (OOP) With JavaScript · Lecture 226: ES6 Classes

Define a `Counter` class with an `inc()` method, and implement `counterAfter(n)` returning the count after calling `inc()` `n` times.

Sample tests

Input: counterAfter(3)
Output: 3
Input: counterAfter(0)
Output: 0

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Declaring count outside the instance, sharing it across instances.

Learning resources

  • MDN: Classes
Approach & explanation (try first)

Each Counter instance holds its own count, incremented by inc().

Loading...
⌘/Ctrl + Enter

Run your code to see results.