Posted by admin at April 10, 2020
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 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
Solution 1
(Procedural approach)
function largestOfFour(arr) {
var results = [];
for (var n = 0; n < arr.length; n++) {
var largestNumber = arr[n][0];
for (var sb = 1; sb < arr[n].length; sb++) {
if (arr[n][sb] > largestNumber) {
largestNumber = arr[n][sb];
}
}
results[n] = largestNumber;
}
return results;
}
results
array.Solution 2
(Declarative approach)
function largestOfFour(arr) {
return arr.map(function(group) {
return group.reduce(function(prev, current) {
return current > prev ? current : prev;
});
});
}
Array.prototype.map()
and return this array as the final resultArray.prototype.reduce()
Solution 3
(Declarative approach)
function largestOfFour(arr) {
return arr.map(Function.apply.bind(Math.max, null));
}
TL;DR: We build a special callback function (using the Function.bind
method), that works just like Math.max
but also has Function.prototype.apply
‘s ability to take arrays as its arguments.
So we want to create a function that does the work of Math.max
and 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.
Function.prototype.apply
method, 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 a Max.max
method. What we’re looking for… almost.
Here we’re passing null
as the context of the Function.prototype.apply
method as Math.max
doesn’t need any context.
arr.map
expects a callback function, not just an expression, we create a function out of the previous expression by using the Function.bind
method.Function.prototype.apply
is a static method of the same Function
object, we can call Function.prototype.bind
on Function.prototype.apply
i.e. Function.prototype.apply.bind
.Function.prototype.apply.bind
call (in this case we want Math.max
so we can gain its functionality).Function.prototype.apply
method will also require a context as it’s 1st argument, we need to pass it a bogus context.null
as the 2nd param to Function.prototype.apply.bind
which gives a context to the Math.max
method.Math.max
is independent of any context, hence, it ignores the bogus context given by Function.prototype.apply
method call.Function.prototype.apply.bind(Math.max, null)
makes a new function accepting the arr.map
values i.e. the inner arrays.Solution 4
(Recursive approach)
function largestOfFour(arr, finalArr = []) {
return !arr.length
? finalArr
: largestOfFour(arr.slice(1), finalArr.concat(Math.max(...arr[0])))
}
Comments