Menu
Coddy logo textTech

Static Shape Counter

Part of the Object Oriented Programming section of Coddy's JavaScript journey. Lesson 42 of 56.

challenge icon

Challenge

Modify the Shape class to track how many shapes have been created using static properties and methods.

Your task:

  1. Add a static property called totalShapes initialized to 0
  2. Increment the counter in the constructor: Shape.totalShapes++;
  3. Add a static method called getTotalCreated() that returns the current count of total shapes

Try it yourself

import { Shape } from './Shape.js';
import { Circle } from './Circle.js';

// Tests
console.log(`Initial count: ${Shape.getTotalCreated()}`); // Should be 0

const shape1 = new Shape('red');
const shape2 = new Shape('green');
const circle1 = new Circle('blue', 5);
const circle2 = new Circle('yellow', 10);

console.log(`After creating 4 shapes: ${Shape.getTotalCreated()}`); // Should be 4

All lessons in Object Oriented Programming

Practice on your own: Online JavaScript compiler