البرمجة

فلترة السفن حسب السعة

To filter the ships by a given capacity, you can create a function that iterates over the fleet array and checks if the capacity of each ship (which is the second element in each inner array) is greater than the given capacity. Here’s how you can do it:

javascript
function filterByCapacity(fleet, capacity) { return fleet.filter(ship => ship[1] > capacity).map(ship => ship[0]); } var filteredShips = filterByCapacity(fleet, 5000); console.log(filteredShips); // Output: ["TITANIC 2"]

In this function:

  • We use the filter method to create a new array containing only the ships whose capacity is greater than the given capacity.
  • We then use the map method to extract the ship names (which are the first elements in the inner arrays) from the filtered array.
  • Finally, we return the array of ship names.

This function will return an array of ship names whose capacity is greater than the given capacity. In the example, it will return ["TITANIC 2"] for a capacity of 5000.

المزيد من المعلومات

في الوظيفة filterByCapacity التي كتبتها، يتم استخدام دالة filter لإنشاء مصفوفة جديدة تحتوي فقط على السفن التي تتجاوز السعة المعطاة. ثم يتم استخدام دالة map لاستخراج أسماء السفن (والتي هي العناصر الأولى في الصفوف الداخلية) من المصفوفة المرشحة. وأخيرًا، يتم إرجاع مصفوفة أسماء السفن.

هذه الوظيفة ستقوم بإرجاع مصفوفة تحتوي على أسماء السفن التي تتجاوز السعة المعطاة. في المثال، سترجع ["TITANIC 2"] لسعة تبلغ 5000.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر