## Where communities thrive

• Join over 1.5M+ people
• Join over 100K+ communities
• Free without limits
##### Activity
Douglas Gray
@dfgray
hint
CamperBot
@camperbot

hint [5/8]

## Hint: 3

Pay close attention to the timing of the storing of variables when working with loops

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Douglas Gray
@dfgray
hint
CamperBot
@camperbot

hint [6/8]

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Douglas Gray
@dfgray
hint
CamperBot
@camperbot

hint [7/8]

## Solution 1:

function largestOfFour(arr) {
var results = [];
for (var n = 0; n < arr.length; n++) {
var largestNumber = 0;
for (var sb = 0; sb < arr[n].length; sb++) {
if (arr[n][sb] > largestNumber) {
largestNumber = arr[n][sb];
}
}

results[n] = largestNumber;
}

return results;
}

# Code Explanation:

• Create a variable to store the results as an array.
• Create an outer loop to iterate through the main array.
• Before going into the inner loop, create a variable to hold the largest number. This must be outside the inner loop.
• Create another for loop to work with the sub-arrays.
• Check if the element of the sub array is larger than the current largest number. If so, then save the number.
• After the inner loop, save the largest number in the variable for the results.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Douglas Gray
@dfgray
hint
CamperBot
@camperbot

hint [8/8]

## Solution 2:

function largestOfFour(arr) {
// You can do this!
// Yes, I can. :P
return arr.map(Function.apply.bind(Math.max, null));
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

# Code Explanation:

• The Function.apply.bind(Math.max, null) makes a new function accepting the arr.map values i.e. the inner arrays.

Now the new function needs to find the max of the input inner array.

• So we want to create a function that does the work of Math.max and accepts input as an array.

e.g. wouldn't be nice if the Math.max would be accepting inputs like this:
Math.max([9, 43, 20, 6]); // 43.

• To do the work of accepting params as array, there is this Function.apply method but it invokes the context function.

i.e. Math.max.apply(null, [9, 43, 20, 6]); // 43 would invoke the Max.max method.

Here we're passing null as the context of the Function.apply method as Math.max doesn't need any context.

But that's not useful for arr.map which accepts a function value. So we create a function value using Function.bind method.

• Since, Function.apply is a static method of Function Object, so we can call Function.prototype.bind on Function.apply i.e. Function.apply.bind.

Now we pass the context for Function.apply.bind call i.e. Math.max method as the 1st argument which would give us the functionality of Math.max method.

• Since Function.apply method takes a context as it's 1st argument, hence, we need to pass a bogus context for Function.apply method.
• So, we pass null as the 2nd param to Function.apply.bind which gives a context to the Math.max method.
• Since, Math.max is independent of any context, hence, it ignores the bogus context given by Function.apply method call.

So in the end we get a function (using Function.bind method) that works like Math.max but accepts params as an array like Function.apply :smiley:

comprendido? :neutral_face:

NOTE: This is an advanced solution. NOT FOR BEGINNERS.

Reference:-

# Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @abhisekp

type hint for next hint :pencil: [Contribute at the FCC Wiki]

DarmawanSM
@DarmawanSM
Bonfire: Return Largest Numbers in Arrays
CamperBot
@camperbot

## :fire:Bonfire: Return Largest Numbers in Arrays :link:

function largestOfFour(arr) {
// You can do this!
return arr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

more info:  bf details | bf links | hint

DarmawanSM
@DarmawanSM
hint
CamperBot
@camperbot

hint [1/8]

## ## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

DarmawanSM
@DarmawanSM
hint
CamperBot
@camperbot

hint [2/8]

## # Explanation:

You will get an array that contains sub arrays of numbers and you need to return an array with the largest number from each of the sub arrays.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

eeerooo
@eeerooo

function largestOfFour(arr) {
var newArray = [];
for (outer = 0; outer < 4; outer++)
{
arr[outer].sort(function(a, b){return b - a});

   for (inner = 0; inner < 4; inner++)
{

if (inner === 0)
{
var largest = sorted[inner];
}

}
newArray.push(largest);

//return arr;
}

return arr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

arr[outer].sort(function(a, b){return b - a}); this is giving me a 'missing semicolon error
Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [1/8]

## ## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [2/8]

## # Explanation:

You will get an array that contains sub arrays of numbers and you need to return an array with the largest number from each of the sub arrays.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [3/8]

## Hint: 1

You will need to keep track of the array with the answer and the largest number of each sub-array.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [4/8]

## Hint: 2

You can work with multidimensional arrays by Array[Index][SubIndex]

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [5/8]

## Hint: 3

Pay close attention to the timing of the storing of variables when working with loops

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [6/8]

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [7/8]

## Solution 1:

function largestOfFour(arr) {
var results = [];
for (var n = 0; n < arr.length; n++) {
var largestNumber = 0;
for (var sb = 0; sb < arr[n].length; sb++) {
if (arr[n][sb] > largestNumber) {
largestNumber = arr[n][sb];
}
}

results[n] = largestNumber;
}

return results;
}

# Code Explanation:

• Create a variable to store the results as an array.
• Create an outer loop to iterate through the main array.
• Before going into the inner loop, create a variable to hold the largest number. This must be outside the inner loop.
• Create another for loop to work with the sub-arrays.
• Check if the element of the sub array is larger than the current largest number. If so, then save the number.
• After the inner loop, save the largest number in the variable for the results.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [8/8]

## Solution 2:

function largestOfFour(arr) {
// You can do this!
// Yes, I can. :P
return arr.map(Function.apply.bind(Math.max, null));
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

# Code Explanation:

• The Function.apply.bind(Math.max, null) makes a new function accepting the arr.map values i.e. the inner arrays.

Now the new function needs to find the max of the input inner array.

• So we want to create a function that does the work of Math.max and accepts input as an array.

e.g. wouldn't be nice if the Math.max would be accepting inputs like this:
Math.max([9, 43, 20, 6]); // 43.

• To do the work of accepting params as array, there is this Function.apply method but it invokes the context function.

i.e. Math.max.apply(null, [9, 43, 20, 6]); // 43 would invoke the Max.max method.

Here we're passing null as the context of the Function.apply method as Math.max doesn't need any context.

But that's not useful for arr.map which accepts a function value. So we create a function value using Function.bind method.

• Since, Function.apply is a static method of Function Object, so we can call Function.prototype.bind on Function.apply i.e. Function.apply.bind.

Now we pass the context for Function.apply.bind call i.e. Math.max method as the 1st argument which would give us the functionality of Math.max method.

• Since Function.apply method takes a context as it's 1st argument, hence, we need to pass a bogus context for Function.apply method.
• So, we pass null as the 2nd param to Function.apply.bind which gives a context to the Math.max method.
• Since, Math.max is independent of any context, hence, it ignores the bogus context given by Function.apply method call.

So in the end we get a function (using Function.bind method) that works like Math.max but accepts params as an array like Function.apply :smiley:

comprendido? :neutral_face:

NOTE: This is an advanced solution. NOT FOR BEGINNERS.

Reference:-

# Credits:

If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @abhisekp

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot
These hints depend on people like you! Please add to this :point_right: Bonfire's Wiki Hints Page
Azamat Kukenov
@azamat89
hint
CamperBot
@camperbot

hint [1/8]

## ## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

CaffeineQueen
@CaffeineQueen
hint
CamperBot
@camperbot

hint [1/8]

## ## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

CaffeineQueen
@CaffeineQueen
hint
CamperBot
@camperbot

hint [2/8]

## # Explanation:

You will get an array that contains sub arrays of numbers and you need to return an array with the largest number from each of the sub arrays.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

CaffeineQueen
@CaffeineQueen
hint
CamperBot
@camperbot

hint [3/8]

## Hint: 1

You will need to keep track of the array with the answer and the largest number of each sub-array.

type hint for next hint :pencil: [Contribute at the FCC Wiki]

momobgood
@momobgood
hint
CamperBot
@camperbot

hint [4/8]

## Hint: 2

You can work with multidimensional arrays by Array[Index][SubIndex]

type hint for next hint :pencil: [Contribute at the FCC Wiki]

momobgood
@momobgood
hint
CamperBot
@camperbot

hint [5/8]

## Hint: 3

Pay close attention to the timing of the storing of variables when working with loops

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Yosafat Ariel
@yoskakomba
hint
CamperBot
@camperbot

hint [1/8]

## ## :construction: After this are possible spoiler hints.

Make sure you've tried to hard to solve it yourself before proceeding. :construction:

type hint for next hint :pencil: [Contribute at the FCC Wiki]

Yosafat Ariel
@yoskakomba
hint
CamperBot
@camperbot

hint [2/8]

## # Explanation:

You will get an array that contains sub arrays of numbers and you need to return an array with the largest number from each of the sub arrays.

type hint for next hint :pencil: [Contribute at the FCC Wiki]