JavaScript Internals (521 Qs)•Part 4: Prototypes, Object Methods & Event Loop (174 - 253)
How do you find the min and max values without Math functions
You can write functions which loop through an array comparing each value with the lowest value or highest value to find the min and max values. Let's create those functions to find min and max values,
javascript
var marks = [50, 20, 70, 60, 45, 30];
function findMin(arr) {
var length = arr.length;
var min = Infinity;
while (length--) {
if (arr[length] < min) {
min = arr[length];
}
}
return min;
}
function findMax(arr) {
var length = arr.length;
var max = -Infinity;
while (length--) {
if (arr[length] > max) {
max = arr[length];
}
}
return max;
}
console.log(findMin(marks));
console.log(findMax(marks));