Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Using Static Properties and Methods in a Class | Classes
Advanced JavaScript Mastery
course content

Course Content

Advanced JavaScript Mastery

Advanced JavaScript Mastery

1. Classes
2. DOM Manipulation
3. Events and Event Handling
4. Asynchronous JavaScript and APIs

bookChallenge: Using Static Properties and Methods in a Class

Task

Imagine you're building an inventory management system for an online store. The Product class needs to keep track of the total number of products added to the inventory, as well as provide functionality to compare the prices of two products.

  1. Define a Static Property:
    • In the Product class, create a static property totalProducts initialized to 0;
    • Each time a new Product instance is created, increment totalProducts by 1 to keep track of how many products have been added to the inventory.
  2. Define a Static Method: Define a static method comparePrices(product1, product2) that takes two Product instances as parameters and returns:
    • "Product 1 is more expensive" if product1 has a higher price;
    • "Product 2 is more expensive" if product2 has a higher price;
    • "Both products have the same price" if they are equal.
12345678910111213141516171819202122232425262728293031
class Product { _____ _____ = 0; // Define static property for total products constructor(name, price) { this.name = name; this.price = price; Product._____+=1; // Increment totalProducts } // Static method to compare prices _____ comparePrices(product1, product2) { if (product1.price > product2.price) { return _____; } else if (_____ < _____) { return _____; } else { return 'Both products have the same price'; } } } // Testing const product1 = new Product('Laptop', 1200); const product2 = new Product('Smartphone', 800); const product3 = new Product('Tablet', 1200); console.log(Product.comparePrices(product1, product2)); // Expected: Product 1 is more expensive console.log(Product.comparePrices(product1, product3)); // Expected: Both products have the same price console.log(Product.comparePrices(product2, product3)); // Expected: Product 2 is more expensive console.log(Product.totalProducts); // Expected: 3
copy
  • Define a static property named totalProducts and initialize it to 0;
  • In the constructor, increment Product.totalProducts by 1 each time a new Product instance is created;
  • Define a static method comparePrices(product1, product2) that takes two parameters: product1 and product2;
  • In comparePrices, use an if statement to check if product1.price is greater than product2.price. If true, return "Product 1 is more expensive";
  • Use an else if statement to check if product1.price is less than product2.price. If true, return "Product 2 is more expensive".
12345678910111213141516171819202122232425262728293031
class Product { static totalProducts = 0; // Define static property for total products constructor(name, price) { this.name = name; this.price = price; Product.totalProducts+=1; // Increment totalProducts } // Static method to compare prices static comparePrices(product1, product2) { if (product1.price > product2.price) { return 'Product 1 is more expensive'; } else if (product1.price < product2.price) { return 'Product 2 is more expensive'; } else { return 'Both products have the same price'; } } } // Testing const product1 = new Product('Laptop', 1200); const product2 = new Product('Smartphone', 800); const product3 = new Product('Tablet', 1200); console.log(Product.comparePrices(product1, product2)); // Output: Product 1 is more expensive console.log(Product.comparePrices(product1, product3)); // Output: Both products have the same price console.log(Product.comparePrices(product2, product3)); // Output: Product 2 is more expensive console.log(Product.totalProducts); // Output: 3
copy

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 13
We're sorry to hear that something went wrong. What happened?
some-alt