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 //