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.
Hint: 1
You will need to keep track of the array with the answer and the largest number of each sub-array.
Hint: 2
You can work with multidimensional arrays by Array[Index][SubIndex]
Hint: 3
Pay close attention to the timing of the storing of variables when working with loops
Spoiler Alert!
Solutions ahead!
First solution
(Procedural approach)
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;
}Second solution
(Declarative approach)
NOTE: This is an advanced solution. NOT FOR BEGINNERS.
function largestOfFour(arr) {
return arr.map(Function.apply.bind(Math.max, null));
}Code Explanation:
First Solution
(Procedural approach)
- Create a variable to store the results as an array.
- Create an outer loop to iterate through the outer array.
- Create a second variable to hold the largest number. This must be outside an inner loop so it won't be reassigned until we find a larger number.
- Create said inner loop to work with the sub-arrays.
- Check if the element of the sub array is larger than the currently stored largest number. If so, then update the number in the variable.
- After the inner loop, save the largest number in the corresponding position inside of the
resultsarray. - And finally return said array.
Second Solution
(Declarative approach)
TL;DR: *We build a special callback function (using the
Function.bindmethod), that works just likeMath.maxbut also hasFunction.apply's ability to take arrays as its arguments *
We start by mapping through the elements inside the main array. Meaning each one of the inner arrays.
Now the need a callback function to find the max of each inner array provided by the map.
So we want to create a function that does the work ofMath.maxand accepts input as an array (which by it doesn't by default).
In other words, it would be really nice and simple if this worked by itself:Math.max([9, 43, 20, 6]); // Resulting in 43
Alas, it doesn't.To do the work of accepting arguments in the shape of an array, there is this
Function.applymethod, but it complicates things a bit by invoking the context function.
i.e.Math.max.apply(null, [9, 43, 20, 6]);would invoke something like aMax.maxmethod. What we're looking for... almost.
Here we're passing
nullas the context of theFunction.applymethod asMath.maxdoesn't need any context.
- Since
arr.mapexpects a callback function, not just an expression, we create a function out of the previous expression by using theFunction.bindmethod. Since,
Function.applyis a static method of the sameFunctionobject, we can callFunction.prototype.bindonFunction.applyi.e.Function.apply.bind.Now we pass the context for the
Function.apply.bindcall (in this case we wantMath.maxso we can gain its functionality).- Since the embedded
Function.applymethod will also require a context as it's 1st argument, we need to pass it a bogus context.- So, we pass
nullas the 2nd param toFunction.apply.bindwhich gives a context to theMath.maxmethod. - Since,
Math.maxis independent of any context, hence, it ignores the bogus context given byFunction.applymethod call. - Thus, our
Function.apply.bind(Math.max, null)makes a new function accepting thearr.mapvalues i.e. the inner arrays.
- So, we pass
Bien noté? ![]()
Reference:-
- http://devdocs.io/#q=js+Math+max
- http://devdocs.io/#q=js+Array+map
- http://devdocs.io/#q=js+Function+apply
- http://devdocs.io/#q=js+Function+bind
Credits:
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @abhisekp @Hallaathrad for your help with Algorithm: Return Largest Numbers in Arrays
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)