I am new to javascript and I don't know how to iterate array of arrays in javascript.
I have a problem where I have to find largest array from an array of array input:
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]
First problem is solved by me successfully whose code I have written at the end but I am not able to solve the second input.
Problem 1 [SOLVED]
Input:
largestOfFour( [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39],[1000,1001, 857, 1]] );Output:
[1000,1001,857,1]
Problem 2 [NOT SOLVED]
Input:
largestOfFour( [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]), [27,5,39,1001] );Output:
[27,5,39,1001]
If still not clear watch this link and tell me thats it
http://freecodecamp.com/challenges/bonfire-return-largest-numbers-in-arrays
My Code for the first problem. (Alter my code so that second can be solved)
function largestOfFour(arr) {
var iAmLarge = new Array();
iAmLarge = arr[0];
var large = iAmLarge[0];
for(var i=0;i<iAmLarge.length;i++) {
if(large<=iAmLarge[i] ) {
large = iAmLarge[i];
}
}
var maxFoundAt = 0;
for(var i=0;i<arr.length;i++){
var newArray=new Array();
newArray = arr[i];
var max = newArray[0];
for(var j=0;j<newArray.length;j++) {
if(max<newArray[j] ) {
max = newArray[j];
}
}
if(max>=large) {
large = max;
maxFoundAt = i;
}
}
alert( arr[maxFoundAt]);
}
largestOfFour( [[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);
[5,27,39,1001]- the highest number from each array