Minimum and Maximum Array Values

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/destr_assign("`Destructuring Assignment`") subgraph Lab Skills javascript/variables -.-> lab-28330{{"`Minimum and Maximum Array Values`"}} javascript/data_types -.-> lab-28330{{"`Minimum and Maximum Array Values`"}} javascript/arith_ops -.-> lab-28330{{"`Minimum and Maximum Array Values`"}} javascript/comp_ops -.-> lab-28330{{"`Minimum and Maximum Array Values`"}} javascript/higher_funcs -.-> lab-28330{{"`Minimum and Maximum Array Values`"}} javascript/destr_assign -.-> lab-28330{{"`Minimum and Maximum Array Values`"}} end

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:

  1. Call reduceWhich with the array you want to process and the optional comparator function.
  2. The reduceWhich function will use Array.prototype.reduce() in combination with the comparator function to return the appropriate element in the array.
  3. 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.

Other JavaScript Tutorials you may like