Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
学ぶ チャレンジ:クラスにおける静的プロパティとメソッドの実装 | JavaScriptクラスと継承の習得
JavaScriptロジックとインタラクション

bookチャレンジ:クラスにおける静的プロパティとメソッドの実装

メニューを表示するにはスワイプしてください

課題

オンラインストアの在庫管理システムを構築することを想定します。Product クラスは在庫に追加された商品の総数を追跡し、2つの商品の価格を比較する機能を提供する必要があります。

  1. 静的プロパティの定義:
    • Product クラス内で静的プロパティ totalProducts0 で初期化;
    • 新しい Product インスタンスが作成されるたびに totalProducts を1増やし、在庫に追加された商品の数を追跡する。
  2. 静的メソッドの定義: 静的メソッド comparePrices(product1, product2) を定義し、2つの Product インスタンスを引数に受け取って、以下を返す:
    • "Product 1 is more expensive": product1 の価格がより高い場合;
    • "Product 2 is more expensive": product2 の価格がより高い場合;
    • "Both products have the same price": 価格が同じ場合。
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
  • totalProducts という名前の静的プロパティを定義し、0 で初期化;
  • コンストラクタ内で、新しい Product.totalProducts インスタンスが作成されるたびに Product を1増やす;
  • 静的メソッド comparePrices(product1, product2) を定義し、2つのパラメータ product1product2 を受け取る;
  • comparePrices 内で、if 文を使って product1.priceproduct2.price より大きいかを判定し、該当する場合は "Product 1 is more expensive" を返す;
  • else if 文を使って product1.priceproduct2.price より小さいかを判定し、該当する場合は "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

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  13

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  13
some-alt