Write a function called findMaxNumber
that takes in an array of numbers and returns the largest number in the array.
/**
* Returns the largest number in an array.
* @param {number[]} arr - The array of numbers.
* @returns {number} - The largest number in the array.
*/
function findMaxNumber(arr: number[]): number;
findMaxNumber([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // 10
findMaxNumber([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]); // 10
findMaxNumber([1, 2, 3, 4, 5, 10, 9, 8, 7, 6]); // 10
- There is a very easy way to do this using a specific built-in method. I would suggest not doing it that way. Try to solve this problem using a
for
loop.
Click For Solution 1
This is the easy way to do it. There is a method called Math.max()
that will return the largest number in an array. This is not the way I would suggest doing it, but it is good to know that this method exists.
function findMaxNumber(arr) {
return Math.max(...arr);
}
There is not too much explanation needed here.
Click For Solution 2
Here is another way of solving it using a for
loop.
function findMaxNumber(arr) {
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
- Create a variable called
max
and setting it equal to the first element in the array. - Loop through the array starting at the second element.
- Check if the current element is greater than the current value of
max
. If it is, we setmax
equal to the current element. - Return
max
after the loop is finished.
test('Finding the maximum number in an array', () => {
expect(findMaxNumber([1, 5, 3, 9, 2])).toBe(9);
expect(findMaxNumber([0, -1, -5, 2])).toBe(2);
expect(findMaxNumber([10, 10, 10, 10])).toBe(10);
});