xxxxxxxxxx
Math.max(1, 2, 3) // 3
Math.min(1, 2, 3) // 1
var nums = [1, 2, 3]
Math.min(nums) // 1
Math.max(nums) // 3
xxxxxxxxxx
x = findMax(1, 123, 500, 115, 44, 88);
function findMax() {
var i;
var max = -Infinity;
for (i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
xxxxxxxxxx
Math.min(x1, x2, ); //Returns the minimum argument
Math.max(x1, x2, ); //Returns the maximum argument
xxxxxxxxxx
function lowest(arr) {
let min = Number.MAX_VALUE
for (let i = 0; i < arr.length; i++) {
let mean = 0
for (let j = 0; j < arr[i].length; j++) {
mean += arr[i][j]
}
mean /= arr[i].length
if (min > mean) {
min = mean
}
}
return min
}
xxxxxxxxxx
const myArray = [{
id: 1,
cost: 200
}, {
id: 2,
cost: 1000
}, {
id: 3,
cost: 50
}, {
id: 4,
cost: 500
}]
const min = Math.min(myArray.map(item => item.cost))
const max = Math.max(myArray.map(item => item.cost));
console.log(min);
console.log(max);
xxxxxxxxxx
if (penyakit === 'flu'){
let obatFlus = database.flu.obat
obatTermurah = ''
cheapest = Number.MAX_VALUE
for (let i = 0; i < obatFlus.length; i++){
let fluObat = obatFlus[i]
let obatName = fluObat[0]
let obatPrice = fluObat[1]
if(obatPrice < cheapest){
obatTermurah = obatName
cheapest = obatPrice
}
}
output = [obatTermurah, cheapest]
}
xxxxxxxxxx
// Array of numbers
const numbers = [5, 2, 9, 3, 1, 7];
// Finding the maximum value
const max = Math.max(numbers);
// Finding the minimum value
const min = Math.min(numbers);
console.log("Maximum value:", max);
console.log("Minimum value:", min);