

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Function that returns the minimum and maximum value of an array in JavaScript
Problem
We are required to write a JavaScript function that takes in an array and return another array, the first element of this array should be the smallest element of input array and second should be the greatest element of the input array.
Example
Following is the code −
const arr = [56, 34, 23, 687, 2, 56, 567]; const findMinMax = (arr = []) => { const creds = arr.reduce((acc, val) => { let [smallest, greatest] = acc; if(val > greatest){ greatest = val; }; if(val < smallest){ smallest = val; }; return [smallest, greatest]; }, [Infinity, -Infinity]); return creds; }; console.log(findMinMax(arr));
Output
[2, 687]
- Related Questions & Answers
- How do I write a function that takes an array of values and returns an object JavaScript?
- Returning an array with the minimum and maximum elements JavaScript
- How to make a function that returns the factorial of each integer in an array JavaScript
- How to find the minimum value of an array in JavaScript?
- Maximum and minimum of an array using minimum number of comparisons in C
- How to find the maximum value of an array in JavaScript?
- Find the maximum possible value of the minimum value of modified array in C++
- Rearrange an array in maximum minimum form by JavaScript
- Find indexes of multiple minimum value in an array in JavaScript
- Get maximum and minimum value in MongoDB?
- Getting Minimum and Maximum Value in MySQL
- Sorting an array that contains the value of some weights using JavaScript
- Display the minimum and maximum value of primitive data types in Java
- How to find the minimum and maximum element of an Array using STL in C++?
- C# program to find maximum and minimum element in an array
Advertisements