Challenge: Implement 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.
- Define a Static Property:
- In the
Productclass, create a static propertytotalProductsinitialized to0; - Each time a new
Productinstance is created, incrementtotalProductsby 1 to keep track of how many products have been added to the inventory.
- In the
- Define a Static Method: Define a static method
comparePrices(product1, product2)that takes twoProductinstances as parameters and returns:"Product 1 is more expensive"ifproduct1has a higher price;"Product 2 is more expensive"ifproduct2has a higher price;"Both products have the same price"if they are equal.
12345678910111213141516171819202122232425262728293031class 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
- Define a static property named
totalProductsand initialize it to0; - In the constructor, increment
Product.totalProductsby 1 each time a newProductinstance is created; - Define a static method
comparePrices(product1, product2)that takes two parameters:product1andproduct2; - In
comparePrices, use anifstatement to check ifproduct1.priceis greater thanproduct2.price. If true, return"Product 1 is more expensive"; - Use an
else ifstatement to check ifproduct1.priceis less thanproduct2.price. If true, return"Product 2 is more expensive".
12345678910111213141516171819202122232425262728293031class 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
すべて明確でしたか?
フィードバックありがとうございます!
セクション 1. 章 13
AIに質問する
AIに質問する
何でも質問するか、提案された質問の1つを試してチャットを始めてください
セクション 1. 章 13