To the Min explainer

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

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

The smaller() function takes in 2 numbers and will output the smaller one. The minimum() function starts by creating a min variable that will store the current minimum, but it will start at 100 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 min and store the smaller one as the new min.

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

Sample code solution:
(Tap below to reveal)

function smaller(a, b) {
  return a < b ? a : b;
}

function minimum(array) {
  let min = 100;
  for (let num of array) {
    min = smaller(min, num);
  }
  return min;
}
console.log(list);
console.log(minimum(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)+10);
const _min = Math.min(...list);
list.splice(pickRandom(3)-1,0,pickRandom(_min)-1);
1 Like

In this puzzel i can’t understand second function. I mean to say that what is the meaning of array infunction minimum
Then what does let min=100 mean
And why console.log(list) is used

3 Likes

Good question,

The minimum function takes an array, and returns the smallest number in that array.

For example, calling the function like this: minimum([35, 11, 75]) will return 11.

Let’s walk through the function.

The min is declared to store the smallest number, and given an initial value of 100.

Then, the for loop loops through the array. Let’s say the array is still [35, 11, 75]. Each number is compared against min. Whichever number is smaller becomes the new value of min.

For example, 35 is compared against the 100 that min was given when it was declared. 35 is smaller than 100, so min now has a value of 35.

The next number, 11, is compared against min, which is 35. 11 is smaller, so min is now 11.

The next number, 75, is compared against 11. 11 is still smaller, so min is still 11.

After the loop is complete, min is returned.

Hope that helps! Let me know if you have any questions.
Ben

1 Like

Hi,
Why

console.log(list);
console.log(minimum(list));

Is used instead of

console.log(minimum(list));

And please write a real example with numbers

Is there anyone can assist???
Or I am alone and there is no one here??
What is the wrong with this code

Or what is the wrong with this

Hi there, it there a YouTube channel that can help me understand the fundamentals of this application?

Sometimes it helps beside reading.

I wish to practice more, somehow I got stuck again :persevere:. Who can recommend me an application that can help me understand coding on the same way grasshopper does?

Hey there, take a look at this post, which has some good recommendations.

1 Like

@Grasshopper_Ben can you explain the hidden code please

Hey there, take a look at this explanation in the thread for the To The Max puzzle, which uses very similar additional code.

1 Like

thanks for the quick support @Grasshopper_Ben sir /teacher