Final Blog

Blogging has been an interesting experience for Fluids class. It’s great that we’re encouraged to find a more creative avenue to contribute to class, but it helps most if you have a focus to free…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Finding the Max Value in an Array

If you are given an array of number values and asked to write a function that returns the max value from that array, how would you do it? Well, I can’t be sure as to how you would do it, but I can walk you through what I did to solve this problem.

Let me start off by saying that this array will not include any negative numbers.

So we start with an array…

One Approach

Let’s also start by writing our function and passing in one parameter which will be used to pass in the array.

Now that we initiated our findMax function let’s start to think of what we can write inside of it. How can we begin solving for this? We know we have to iterate through the array so let’s set that up.

I wrote a for…of loop (introduced in ES2015) to iterate through the array and set num to be each value in the array. So as we iterate through each value in the array we will have to compare the current num value to the previous num value in the array to see what’s greater. The greater of the two values will have to be stored. We can set up a variable outside of the for…of loop for this. Let’s do that now…

The variable we created is called max and initialized with the value of -1. Why -1? As I mentioned before, we do not have to worry about the array including negative values, so therefore we can start with max being -1 and not worry about the first value in the array being less than it. This will make more sense in a moment. We now can set up an if statement within the for…of loop. This will allow our max variable to only be reassigned if the current num value is greater than what max currently is. Let’s set that up…

Let’s walkthrough what we have going on inside the function up to this point. For this walkthrough lets pass in the array [56,45,88] to our findMax function.

All we have to do now after the for…of loop is return max

Another Approach

If we use JavaScript’s built in sort() method we can find the max value even easier. Also, even if all the values in the array were negative values this approach would still work. Our first approach would fail to work if that was the case.

Here is the approach #2…

So essentially we are using the sort method to first sort the array putting the max value as the last index in that array. Then we just return the last index of the array by whatever the length of the array is minus 1.

Add a comment

Related posts:

Pirate Summit 2018

In what must be one of the most unique events I have the privilege of documenting each year, the Cologne, Germany based Pirate Summit is always a treat. Held in an artistic junkyard(?) or is a junky…

8 predictions for Instagram in 2018

The first few weeks of 2018 has already seen the engagement rates of most accounts spiralling downwards and when it comes to the mystery of the Instagram algorithm there is lots of speculation about…

The Biggest Mistake I Made in Business

When I first came to Handel Group as a client, many years back, my chief complaint was that my business had plateaued. Coming off of great success the prior year, my business partner (and twin…