JavaScript: Return the minimum-maximum value of an array, after applying the provided function to set comparing rule
JavaScript fundamental (ES6 Syntax): Exercise-15 with Solution
Min-Max Value of Array with Function
Write a JavaScript program to return the minimum-maximum value of an array, after applying the provided function to set a comparing rule.
- Use Array.prototype.reduce() in combination with the comparator function to get the appropriate element in the array.
- Omit the second argument, comparator, to use the default one that returns the minimum element in the array.
Sample Solution:
JavaScript Code:
Output:
1 30 {"name":"Kevin","age":16}
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that applies a given function to each element of an array and returns both the minimum and maximum results.
- Write a JavaScript function that maps an array using a comparator function, then extracts the min and max values.
- Write a JavaScript program that finds the minimum and maximum of an array after transforming its elements via a provided callback.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript program to replace the names of multiple object keys with the values provided.
Next: Write a JavaScript function that returns true if the provided predicate function returns true for all elements in a collection, false otherwise.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.