To the Max explainer

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

Walkthrough of the solution: In this lesson, we are importing list which does not appear in the lesson, but can be seen below in the hidden code. list is just an array of 5 different numbers.

In order to find the largest number in the list array, we will use the function larger(), which just takes in 2 numbers and returns the larger of the two:

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

If a is greater than b, the funtion returns a, if not it returns b. So now the question is, where do a and b come from? We put in a and b in the following function maximum():

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

Now we’re using a function within another function, so a is max which starts as zero, and b is num, which is the first number in the list array from the hidden code. To help clarify this, we’ll use the following array as an example argument for list:

list = [44, 29, 7, 88, 31];

The first time the for…of loop iterates through list, the first number is 44, and we know that max starts as 0, so think of the function like this:

function maximum([44, 29, 7, 88, 31]) {
    let max = 0;
    for (let num of [44, 29, 7, 88, 31]) {
        max = larger(0, 44);  -----> a is 0, b is 44
    }
    return max;
}

Because 44 is great than 0, max now becomes 44, and the for…of loop continues to the next number in the array:

function maximum([44, 29, 7, 88, 31]) {
    let max = 0;
    for (let num of [44, 29, 7, 88, 31]) {
        max = larger(44, 29);  -----> a is now 44, b is now 29
    }
    return max;
}

Because 44 is greater than 29, the value of max remains as 44 and the for…of loop continues. max will remain 44 unless a bigger number in the array is found, so in the array:

list = [44, 29, 7, 88, 31];

we can see that ultimately max will become 88 as it is the biggest number in the array. By using a simple function that returns the bigger of 2 numbers over and over with a for…of loop, we have created a program that finds the biggest number in an array:

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 operator), console.log(), Code Block (for loop)
Additional Code (hidden code that runs before the puzzle’s code):
The purpose of the hidden code is simply to provide us with an array of 5 numbers, or list to use in this lesson. If you would like to see how that

const randomNumber = pickRandom(4);
// The pickRandom(4) function generates a random number between 1 and 4, and this is stored in the randomNumber variable //
const index = randomNumber - 1;
// We want randomNumber as the index we will use with the arrayOfArrays array below. We're subtracting 1 because arrays start at 0, so the index variable will be either 0, 1, 2 or 3.//
const arrayOfArrays = [
        [44, 29, 7, 88, 31],
        [14, 93, 56, 72, 5],
        [63, 19, 37, 49, 28],
        [98, 85, 76, 51, 33]
    ];
//One of these 4 arrays will be chosen at random //
const list = arrayOfArrays[index];
// The `list` variable now holds the array `arrayOfArrays` at an index that is randomly 0 - 3 //
11 Likes

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

12 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

51 Likes

Thanks! Great explanation!

8 Likes

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

5 Likes

Hey there,

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

–Ben

11 Likes

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.

2 Likes

Hey there,

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

Ben

3 Likes

Hidden code causes some confusion.

15 Likes

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.

12 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?

5 Likes

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?

4 Likes

Same for me too. This was hard to understand.

2 Likes

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.

2 Likes

@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

6 Likes

@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.

4 Likes

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.

3 Likes

4 Likes

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

4 Likes

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?

6 Likes