I think I can guess what the issue is and your confusion. I can't see the prompt, but the way it's written it looks at first like they want you to find a single value--the max out element, the ultimate max of an array of arrays, for example. At first glance that's what I thought until I read it more carefully--it looks like they want you to give an array of the max elements of arrays. So, at first glance:

constarrayGroup=[[1,2,3,4],[2,4,6,8],[3,6,9,12],[32,44,66,11]];// what I expected waslargestOfFour(arrayGroup);// => 66// what they wanted waslargestOfFour(arrayGroup);// => [4,8,12,66]

You can't return arr[n][sb] as is for two reasons in the above code:

(arr[n][sb] > largestNumber) being true means that element is currently larger than the largestNumber and all other elements that have preceded it--but NOT the elements after. Imagine you had an array [1,2,3,4] -- 2 is larger than 1, but if you return that then since you've found a larger number, you miss the others. You've proved that it is larger but not THE largest since you have no knowledge at that point of the numbers after it.

... I forgot what number two is. :P

FWIW, I found it confusing too; both the first time I did something like that last semester and in reading their code. Also I don't love their naming style....

Anywho:

// their code, annotated by mefunctionlargestOfFour(arr){varresults=[];// loop through our arraysfor(varn=0;n<arr.length;n++){// make largestNumber the first item of the arrayvarlargestNumber=arr[n][0];// compare it to the other items in that same arrfor(varsb=1;sb<arr[n].length;sb++){// if the currentNumber is bigger than the largestNumber// then it itself is the new largest number we compare to.if(arr[n][sb]>largestNumber){largestNumber=arr[n][sb];}}// add it to our results arrayresults[n]=largestNumber;}// return the arrayreturnresults;}// ====================================================================// how I'd write it...// longer, but clearer, I think!functionhowIdWriteIt(groupOfArrays){varlargestNumberArray=[];// loop through our arraysfor(varwhichArray=0;whichArray<groupOfArrays.length;whichArray++){varcurrentArray=groupOfArrays[whichArray];varlargestNumber=currentArray[0];for(varwhichNumber=1;whichNumber<currentArray.length;whichNumber++){varcurrentNumber=currentArray[whichNumber];if(currentNumber>largestNumber){largestNumber=currentNumber;}}// add it to our largestNumberArray array with the same index as the array we're on -- so it matches up// ie, the 0th entry in largestNumberArray is the max number of the 0th arraylargestNumberArray[whichArray]=largestNumber;}// return the arrayreturnlargestNumberArray;}// and if you wanna test...!constarrayGroup=[[1,2,3,4],[2,4,6,8],[3,6,9,12],[32,44,66,11]];console.log(largestOfFour(arrayGroup));console.log(howIdWriteIt(arrayGroup));

And for good measure, what I had thought they wanted and how I would've written it:

functiongetMaxOfAllArrays(groupOfArrays){varlargestNumberArray=[];varlargestNumber=0;// picking 0 to start// loop through our arraysfor(varwhichArray=0;whichArray<groupOfArrays.length;whichArray++){varcurrentArray=groupOfArrays[whichArray];// index change -- since we are no longer comparing within the array// we need to loop through the whole thing.for(varwhichNumber=0;whichNumber<currentArray.length;whichNumber++){varcurrentNumber=currentArray[whichNumber];if(currentNumber>largestNumber){largestNumber=currentNumber;// we can't stop here--what about the other arrays?}}}// return the maxreturnlargestNumber;}

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].

My solution:

function largestOfFour(arr) {
for (i = 0; i < arr.length; i++) {
var largest = arr[i][0];
for (j = 0; j < arr[i].length; j++) {
if (arr[i][j] > largest) {
return arr[i][j];
}
}
}

Why do I need to have an empty bracket set for largestNumberArray, per your example? I'm not exactly clear on that part.

Let me go through your solution line by line with some comments, hopefully it'll make you better understand the issue with the code.

Problem:

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].

functionlargestOfFour(arr){// We iterate over every sub-array.for(i=0;i<arr.length;i++){// Mark the first element as largest for now.varlargest=arr[i][0];// Ok, now we can step through all the elements that are in the// sub-array, to try and find the actual largest one.for(j=0;j<arr[i].length;j++){// So if the element we currently look at is bigger than the// previously largest, it must be our new largest.if(arr[i][j]>largest){// Now here is our problem, instead of making it our new largest item// for that sub-array, we instantly return it, ending our function!//// This means that we don't check any other elements of the current// sub-array if they are even larger.//// This also means that we never even get to start checking the// other sub-arrays at all.returnarr[i][j];}}}}

After returning this one element the whole function ends! It doesn't do any more work. The only way to solve this problem with loops, is to build up the whole largestNumberArray first, and then when you're finished with everything return it at the very end of the function.

## re: Explain This Like I'm Five VIEW POST

FULL DISCUSSIONWhat did you write as your solution?

I think I can guess what the issue is and your confusion. I can't see the prompt, but the way it's written it looks at first like they want you to find a single value--the max out element, the ultimate max of an array of arrays, for example. At first glance that's what I thought until I read it more carefully--it looks like they want you to give an array of the max elements of arrays. So, at first glance:

You can't return

`arr[n][sb]`

as is for two reasons in the above code:`(arr[n][sb] > largestNumber)`

being true means that element is currently larger than the largestNumber and all other elements that have preceded it--but NOT the elements after. Imagine you had an array [1,2,3,4] -- 2 is larger than 1, but if you return that then since you've found a larger number, you miss the others. You've proved that it is larger but not THE largest since you have no knowledge at that point of the numbers after it.... I forgot what number two is. :P

FWIW, I found it confusing too; both the first time I did something like that last semester and in reading their code. Also I don't love their naming style....

Anywho:

And for good measure, what I had

thoughtthey wanted and how I would've written it:The problem:

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].

My solution:

function largestOfFour(arr) {

for (i = 0; i < arr.length; i++) {

var largest = arr[i][0];

for (j = 0; j < arr[i].length; j++) {

if (arr[i][j] > largest) {

return arr[i][j];

}

}

}

Why do I need to have an empty bracket set for largestNumberArray, per your example? I'm not exactly clear on that part.

Let me go through your solution line by line with some comments, hopefully it'll make you better understand the issue with the code.

Problem:

After returning this one element the whole function ends! It doesn't do any more work. The only way to solve this problem with loops, is to build up the whole

`largestNumberArray`

first, and then when you're finished with everything return it at the very end of the function.