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.
You will need to keep track of the array with the answer and the largest number of each sub-array.
You can work with multidimensional arrays by Array[Index][SubIndex]
Pay close attention to the timing of the storing of variables when working with loops
Solutions ahead!
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;
}- 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.
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]]);- The
Function.apply.bind(Math.max, null)makes a new function accepting thearr.mapvalues 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.maxand 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.applymethod 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
nullas the context of theFunction.applymethod asMath.maxdoesn'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.applyis a static method ofFunctionObject, so we can callFunction.prototype.bindonFunction.applyi.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.applymethod takes a context as it's 1st argument, hence, we need to pass a bogus context forFunction.applymethod.- 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.
- So, we pass
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 😃
comprendido? 😐
NOTE: This is an advanced solution. NOT FOR BEGINNERS.
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
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @abhisekp
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.)