xxxxxxxxxx
function findMinMax(arr) {
if (arr.length === 0) {
return "Array is empty.";
}
let min = arr[0];
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
return { min, max };
}
// Example usage:
const numbers = [11, 5, 8, 3, 20, 16];
const result = findMinMax(numbers);
console.log(result); // Output: { min: 3, max: 20 }
xxxxxxxxxx
// find mimimum value of array in javascript
// array reduce method
const arr = [49,2,71,5,38,96];
const min = arr.reduce((a, b) => Math.min(a, b));
console.log(min); // 2
// math.min apply method
const min_ = Math.min.apply(null, arr);
console.log(min_); // 2
// or math.min spread operator method
const min__ = Math.min(arr);
console.log(min__); // 2
xxxxxxxxxx
const dates = [];
dates.push(new Date('2011/06/25'));
dates.push(new Date('2011/06/26'));
dates.push(new Date('2011/06/27'));
dates.push(new Date('2011/06/28'));
const maxDate = new Date(Math.max.apply(null, dates));
const minDate = new Date(Math.min.apply(null, dates));
xxxxxxxxxx
// Assuming the array is all integers,
// Math.min works as long as you use the spread operator(...).
let arrayOfIntegers = [9, 4, 5, 6, 3];
let min = Math.min(arrayOfIntegers);
// => 3
xxxxxxxxxx
//get min/max value of arrays
function getArrayMax(array){
return Math.max.apply(null, array);
}
function getArrayMin(array){
return Math.min.apply(null, array);
}
var ages=[11, 54, 32, 92];
var maxAge=getArrayMax(ages); //92
var minAge=getArrayMin(ages); //11
xxxxxxxxxx
var numbers = [1, 2, 3, 4];
Math.max(numbers) // 4
Math.min(numbers) // 1
xxxxxxxxxx
public static void main(String[] args) {
int[] xr = {2, 4, 1, 3, 7, 5, 6, 10, 8, 9};
//find maximum value
int max = xr[0];
for (int i = 0; i < xr.length; i++) {
if (xr[i] > max) {
max = xr[i];
}
}
//find minimum value
int min=xr[0];
for (int i = 0; i <xr.length ; i++) {
if (xr[i]<min){
min=xr[i];
}
}
System.out.println("max: "+max);
System.out.println("min: "+min);
}
xxxxxxxxxx
// find maximum value of array in javascript
// array reduce method
const arr = [49,2,71,5,38,96];
const max = arr.reduce((a, b) => Math.max(a, b));
console.log(max); // 96
// math.max apply method
const max_ = Math.max.apply(null, arr);
console.log(max_); // 96
// or math.max spread operator method
const max__ = Math.max(arr);
console.log(max__); // 96
xxxxxxxxxx
var array = [3, 1, 5, 2, 4];
var minValue = Math.min(array);
console.log(minValue); // Output: 1