To the Max explainer

The aim of this puzzle: Find the largest number in the array using a loop and the larger() function.

Walkthrough of the solution: To find the maximum of a list, you can compare the first 2 items and store larger one as the current maximum. Then you compare that current maximum to the 3rd item, and store the larger one. Then continue until you’ve compared each item to the current maximum. In the end, the maximum will be the largest value from the array.

The larger() function takes in 2 numbers and will output the larger one. The maximum() function starts by creating a max variable that will store the current maximum, but it will start at 0 for now. The loop will go through each item in the array and hold it in a variable called num. What we need to do is compare the num to the max and store the larger one as the new max.

Instead of max = num, we need to use max = larger(max, num). You can also switch the order of the arguments.

Sample code solution:
(Tap below to reveal)

function larger(a, b) {
  return a > b ? a : b;
}

function maximum(array) {
  let max = 0;
  for (let num of array) {
    max = larger(max, num);
  }
  return max;
}
console.log(list);
console.log(maximum(list));

JavaScript Concepts: Binary Expression (>), Code Block (function), Conditionals (ternary statement), console.log(), Code Block (for loop)
Additional Code (hidden code that runs before the puzzle’s code):

const list = [...Array(4)].map(i=>pickRandom(90));
const _max = Math.max(...list);
list.splice(pickRandom(3)-1,0,_max+pickRandom(100-_max));
2 Likes

This one i found hard to understand.
Both functions are not clear to me… Could you please elaborate?

2 Likes

Hey there, let’s take a look at how the functions work.

function larger(a, b) {
  return a > b ? a : b;
}

The larger function takes 2 numbers and returns whichever one is largest.

It calls these numbers a and b in the function declaration. If a > b, then the function returns a. Otherwise it returns b.

The maximum function takes an entire array of numbers, and returns the largest number in the array.

function maximum(array) {
  let max = 0;
  for (let num of array) {
    max = larger(max, num);
  }
  return max;

Let’s call the function with [3, 6, 2] as an argument:

When the function looks at the 3, max is 0. It uses the larger() function to determine which is greater:

max = larger(0, 3);

3 is larger, so max is updated to be 3.

When the function looks at the 6, max is 3, so it calls:

max = larger(3, 6);

6 is larger, so max is updated to be 6.

Finally, the function looks at the 2. max is 6, so it calls:

max = larger(6, 2);

6 is larger, so max is still 6.

Now that all of the numbers have been looked at, max is returned. Therefore, maximum([3, 6, 2]) returns 6.

Hope this clears things up! Let me know if you have any questions.
Ben

9 Likes

Thanks! Great explanation!

1 Like

Hi, I’m Emmanuel from México, is there any code hiden, like the array, or list or maximum?
Thanks

Hey there,

The list array is declared in hidden code. It contains 4 random numbers between 0 and 90.

–Ben

1 Like

Hi, Can you give a detailed explanation of this topic. I understand the first set of codes but Im having trouble understanding the remaining. I will be very thankful if you do so.

Hey there,

Please see this post earlier in this thread for an explanation.

Ben

Hidden code causes some confusion.

1 Like

Agreed. The part that created confusion for me was that there was hidden code and the little “hint” button did not state there was hidden code. In previous steps it stated there was hidden code.

I appreciate the free coding lessons, thank you muches, but please state when there is hidden code… Please.

Without at least knowing there is hidden code, it’s like having a math teacher go through an example on the board and not showing all the work. Where as if I know there is hidden code, I can at least rationalize what might be happening behind the scenes to understand the problem.

Again, thank you for the website.

2 Likes

I have a question.

In the code, why can’t we just use :

max = larger(max,array)

And why do we need to list all the elements of the array and then use the code?

Why using the array directly in the function does not work?

What I don’t understand : if we’re using an array from a var (or is it a function?) called list, why dont we write ‘list’ instead of ‘array’? Or vice versa?
Would it be possible to see the hidden code for both list and array?

Same for me too. This was hard to understand.

Hey, is there any place to see the hidden code? I would love to know if what I think it’s supposed to be is right.

@Zandy
Hello!
There is a hidden code which generates 4 random numbers between 0-90 and stores it to ‘list’ array

For your understanding, it is something like:

var list =[
pickRandom(‘90’)
pickRandom(‘90’)
pickRandom(‘90’)
pickRandom(‘90’)
]

This is not the exact code used but would explain what the hidden code did.

pickRandom () doesn’t work elsewhere except in the grasshopper app

@DinalaS

I see what your problem is… It looks like the concept of functions is not clear to you.

‘list’ is an array containing 4 numbers

Just for your understanding… Take this example…

function example(a) {
console.log(a);}

The above function says that “Take anything that is used as an argument when the function’s name is taken” and the code inside the function says that “console.log anything that is used as an argument when the function’s name is taken”

So if I write

function example(a) {
console.log(a);}
example(‘Hello’)

The output will be
Hello

Because now a = hello

Please kindly enlighten me if you think I am wrong.

Wow, thanks! It is sort of what I imagined. I’m glad to see something is clicking in my brain and I’m getting it.

Thank you so much, again.

After keeps on trying for the second day, take pressure and, i solve it but pity, unluckyme and feel upsad cause
I still dont understand

Can you please explain this for me. I DON’T UNDERSTAND the a and b. Where is that coming from? Does it hold a value and what does the Computer read it as?