Currently, your function actually doesn't work. For example, if you input [1, 200, 3, 400]
it will result in 200
not 400
.
The purpose of this task seems to be to get you thinking about how you can implement an algorithm to find the maximum value in an array, hence the ban on Math
functions.
When approached with such a task it is important to think about it logically and sequentially. Sometimes it can be useful to think about how you, as a human, would solve such a problem. It can be easier to write your algorithm/function when you have done this and broken down the logic into steps. When going through the array you need to do the following (steps):
- Check if the current number is greater than the current maximum number.
- If this is the case, set the maximum number equal to the current number
- If this is not the case, you don't need to do anything. Instead, let your loop proceed to the next number in your array
- Repeat this cycle until you have looked at all numbers in your array.
That is the basic outline of how your algorithm should function. However, you may run into a few issues with this. For instance, when looking at the first number in your array what should be considered the current maximum number (as you haven't seen any previous values yet)? This problem can be solved by setting the current maximum number to the first element in your array at the beginning and then running your algorithm to find any number bigger than the maximum number (the first number) and updating its value accordingly (when a higher number is found).
Taking all this into consideration your findMax()
function can be rewritten like so (read code comments for further explanation):
function findMax(myArray) {
var maxNumber = myArray[0]; // when we first start, we can set the first element in our array to be the max number - it might not be the max number in the end, but it is a number we can compare the rest of the numbers in our array off of.
for (var i = 1; i < myArray.length; i++) { // loop through our array. (we can start i at 1, because we don't need to check the first element as we have set it to the maxNumber above)
var currentNumber = myArray[i]; // get the current number from the array.
if(currentNumber > maxNumber) { // if the current number is greater than our maximum number (which we initially set to the first number in our array) we can change our maxNumber to be the currentNumber:
maxNumber = currentNumber
}
}
// When we run this code our loop would've finished, meaning we have gone through our entire array and found the maxNumber
return maxNumber;
}
console.log(findMax([1, 200, , 3, 400])); // Output: 400
Now if you are being really pedantic about making your function work exactly like Math.max()
you can see this answer which shows Chrome V8's implementation of this method.
This here is just one implementation on how you can implement an algorithm to find the highest value in an array.
If you are worried about performance it is also important to understand that condensed code doesn't necessarily equal better performance.
The solution I provided here has a time complexity of O(N), where N is the number of elements in your array. This means that in order to find the highest element in your array it needs to check every element in your array. This might not sound too bad, but if your array grows in size (say it has 1, 000, 000 numbers), this can take some time to calculate. However, the truth of the matter is, in order to find the highest number in an array you do in fact need to check every element.
However, with that being said, there are other data structures (such as a max-heap) you can implement which can provide you with better time complexity and performance. If you were for instance to use a 'max-heap' and all you wanted to do was find the max number, you could use it in such a way which allows you to find the max number (ie get the root node) in O(1) time complexity. This means that you can instantly get the maximum number from your data structure. This isn't anything that you need to implement per se, but rather it just something to know and have in the back of your mind.