Introduction
In this lab, we will explore how to find the minimum and maximum values of an array based on a provided function. We will use the reduce() method in combination with a comparator function to return the appropriate element in the array. By the end of the lab, you will have a good understanding of how to use this method to find the minimum and maximum values in an array based on your custom comparator function.
How to Find the Min and Max of an Array Using a Provided Function
To practice coding, open the Terminal or SSH and type node.
Here's a function that returns the minimum and maximum values of an array, based on a provided function that sets the comparison rule:
const reduceWhich = (arr, comparator = (a, b) => a - b) =>
arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a));
To use it, follow these steps:
- Call
reduceWhichwith the array you want to process and the optionalcomparatorfunction. - The
reduceWhichfunction will useArray.prototype.reduce()in combination with thecomparatorfunction to return the appropriate element in the array. - If you omit the second argument (
comparator), the default function will be used, which returns the minimum element in the array.
Here are some examples of how to use reduceWhich:
reduceWhich([1, 3, 2]); // 1
reduceWhich([1, 3, 2], (a, b) => b - a); // 3
reduceWhich(
[
{ name: "Tom", age: 12 },
{ name: "Jack", age: 18 },
{ name: "Lucy", age: 9 }
],
(a, b) => a.age - b.age
); // {name: 'Lucy', age: 9}
In the examples above, the first call to reduceWhich returns the minimum value of the array [1, 3, 2], which is 1. The second call returns the maximum value of the same array, based on the comparator function that reverses the order of comparison. The third call returns the object in the array that has the minimum age property, based on the comparator function that compares the age properties of the objects.
Summary
Congratulations! You have completed the Min and Max of Array Based on Provided Function lab. You can practice more labs in LabEx to improve your skills.