
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- How to make a function that returns the factorial of each integer in an array JavaScript
- 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
- Rearrange an array in maximum minimum form by JavaScript
- How to find the minimum value of an array in JavaScript?
- How to find the maximum value of an array in JavaScript?
- Maximum and minimum of an array using minimum number of comparisons in C
- Find indexes of multiple minimum value in an array in JavaScript
- Find the maximum possible value of the minimum value of modified array in C++
- How to call a function that returns another function in JavaScript?
- How to remove elements from an array until the passed function returns true in JavaScript?
- Get maximum and minimum value in MongoDB?
- Getting Minimum and Maximum Value in MySQL
- C Program to Minimum and Maximum prime numbers in an array
- Rearrange an Array in Maximum Minimum Form using C++

Advertisements