MAX-MIN

Question : Write a program to find the min/max element of an array.

Method 1

//Using inbuilt functions
const arr = [20, -3, -15, -20, 3, 15, 40];
function minMax(arr) {
const min=Math.min(...arr);
const max=Math.max(...arr);
return [min, max]
}
console.log(minMax(arr));
//Prints: [-20, 40]

Method 2

//Without using inbuilt functions
const arr = [20, -3, -15, -20, 3, 15, 40];
function minMax(arr) {
let min=arr[0], max=arr[0];
for (let i = 1; i < arr.length; i++) {
let value = arr[i];
min = (value < min) ? value : min;
max = (value > max) ? value : max;
}
return [min, max];
}
console.log(minMax(arr));
//Prints: [-20, 40]