Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].
function largestOfFour(arr) {
return arr;
}
largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1]]
);
Here’s my solution, with embedded comments to help you understand it:
function largetstOfFour(arr) {
//Step 1. Create an array that will host the result of the 4 sub arrays
var largestNumber =[0,0,0,0];
//Step2. Create the first FOR loop that will iterate through the arrays
for(var arrayIndex =0; arrayIndex < arr.length; arrayIndex ++) {
/*The starting point, index 0, corresponds to the first array */
//Step 3. Create the second FOR loop that will iterate through the sub-arrays
for(var subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex++){
/*The starting point, index 0, corresponds to the first sub-array */
if(arr[arrayIndex][subArrayIndex] > largestNumber[arrayIndex]) {
largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
}
}
}
//Step 4. Return the largetst numbers of each sub-array
return largestNumber;// largestNumber = [5,27,39,1001];
}
largestOfFour([[4,5,1,3], [13,27,18,26], [32,35,37,39],[1000,1001,857,1]]);