Actually, JavaScript provides two inbuilt methods to find the highest and lowest number in an array. This article will first introduce these methods, and then explain how they work in simple terms.
1. Highest(Max) number
Javascript provides the Math.max() method to find the highest values in an array.
const numbers = [2, 9, 3, 5, 7, 8];
const maxNumber = Math.max(...numbers);
console.log(maxNumber); // Output: 9
How Math.max() method works ?
Lets say you have an array(numbers) and you want to get the maximum number.
to do that, we need to first create a function called max(). Than we will create a variable called “largest
” and assign the first element of the numbers
array (numbers[0]
) to the largest
variable. This sets an initial value for largest
.
Now, just assume that the first element of the array(numbers) which we assigned to the variable “largest” is the biggest number. Than we will loop through the remaining elements of the numbers
array using a for
loop. We will start the loop at index 1
(the second element) because we already assigned the first element to “largest
“.
For each element in the loop, we compare it with the current value of largest
. If the element is greater than largest
, we update the value of largest
to that element.
After the loop finishes, we have found the largest element in the numbers
array. We return the value of largest
as the output of the function max().
So, if you call the max()
, it will return 9
, which is the largest number in the numbers
array.
Code :
const numbers = [2, 9, 3, 5, 7, 8];
function max() {
let largest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > largest) {
largest = numbers[i];
}
}
return largest;
}
console.log(max(numbers)); // Output: 9
2. Lowest(Min) number
Javascript provides the Math.min() method to find the lowest values in an array.
const numbers = [2, 9, 3, 5, 7, 8];
const minNumber = Math.min(...numbers);
console.log(minNumber); // Output: 2
How Math.min() method works ?
Here, we will do the same thing that we did to find the highest value but with a small change.
Lets say you have an array(numbers) and you want to get the smallest number.
To do that, we will create a function called min(). Than we will create a variable called “lowest
” and assign the first element of the numbers
array (numbers[0]
) to the "lowest"
variable. This sets an initial value for "lowest"
.
Now, just assume that the first element of the array(numbers) which we assigned to the variable “lowest” is the smallest number. Than we will loop through the remaining elements of the numbers
array using a for
loop. We will start the loop at index 1
(the second element) because we already assigned the first element to “l
owest”.
For each element in the loop, we compare it with the current value of "lowest"
. If the element is less than "lowest"
, we update the value of "lowest"
to that element.
After the loop finishes, we have found the smallest element in the numbers
array. We return the value of "lowest"
as the output of the function min().
So, if you call the min()
, it will return2
, which is the smallest number in the numbers
array.
const numbers = [2, 9, 3, 5, 7, 8];
function min() {
let lowest = numbers[0];
for (let i = 1; i < numbers.length; i++) {
if (numbers[i] > lowest) {
lowest = numbers[i];
}
}
return lowest;
}
console.log(min(numbers)) // Output : 2