Menu
Coddy logo textTech

Smart Home Device System

Part of the Object Oriented Programming section of Coddy's JavaScript journey — lesson 56 of 56.

challenge icon

Challenge

In this challenge you have to add static tracking to the SmartDevice system so we can monitor how many devices are in our smart home.

1. Add static property to SmartDevice class:

  • Add: static totalDevices = 0; at the top of the class

2. Increment the counter in constructor:

  • In the SmartDevice constructor, add: SmartDevice.totalDevices++;

3. Add static method to get count:

  • Add: static getDeviceCount() { return SmartDevice.totalDevices; }

Try it yourself

import { SmartDevice } from './device.js';

console.log(`Device count: ${SmartDevice.getDeviceCount()}`); // Should be 0

const basicDevice1 = new SmartDevice('Smart Plug');
const basicDevice2 = new SmartDevice(' Thermostat');
console.log(`Total devices now: ${SmartDevice.getDeviceCount()}`); // Should be 2

All lessons in Object Oriented Programming