# Return Largest Numbers in Arrays ?:(

Return Largest Numbers in Arrays ?:(
0

#1

Tell us what’s happening:

Hi Guys,

Really stuck here have read and read. I’d like to try find the solution using roughly this rather than using the Math.max which I have read about

``````
function largestOfFour(arr) {
// You can do this!
var largeArr = [];

for ( var i = 0; i < arr.length; i++ ) {
var largestNumber = arr[i][0];
for (var j = 0; j < arr[i].length; j++) {
if(arr[i][j] > largestNumber) {
largeArr.push(arr[i][j]);

}
}

}

return largeArr[i];
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36`.

#2

Hey DannyD!

Great start so far. It looks like you intend on storing the largest numbers in `largeArr`, is that correct?

Here’s something to think about that my help you understand what’s going on. Try reading through your code line by line to figure out what it will do at `i=0` if `arr[0]` is equal to `[0, 1, 2, 3]`. What does `largestNumber` equal? as you loop through the inner for loop (that uses `j`), what is in `largeArr` when `j=0`, what about when `j=1`?

Again, great setup so far; if you’re stuck understanding it, feel free to add another comment and I’ll help you out

#3
``````
Hi Josh, thanks for taking the time to get back to me

I think I get where you are going with the j = 1 in that we want to start from j = 1 rather than j = 0 as this is accounted for in the arr[i][0]. Is this what you were getting at?

I have also changed the code so there is no longer a .push() involved as I think the answers get into largeArr any way through largeArr[i]. Could be wrong

Thanks

function largestOfFour(arr) {
// You can do this!
var largeArr = [];

for ( var i = 0; i < arr.length; i++ ) {
var largestNumber = arr[i][0];
for (var j = 1; j < arr[i].length; j++) {
if(arr[i][j] > largestNumber) {
largestNumber = arr[i][j];

}
}

}

return largeArr[i];
}

``````

#4

This is really close!

First, give your return statement another look. When you say `return largeArr[i]`, you only return the value at the ith position of largeArr[i]. For example if largeArr contained the following numbers [5, 1, 3] and i was equal to 2. `return largeArr[i]` would return `3`. So with your current code you’re only returning a single number at best.

Second, you still need to add `largestNumber` to `largeArr` somewhere.
Hint: This doesn’t happen on your `return` line.
Hint: Using `.push()` isn’t the worst idea

Keep at it! You’re very close.

#5
``````
Hi Morganda, thanks for getting back to me

I've made a few changes, let me know what you think

Thanks

function largestOfFour(arr) {
// You can do this!
var largeArr = [];

for ( var i = 0; i < arr.length; i++ ) {
var largestNumber = arr[i][0];
for (var j = 1; j < arr[i].length; j++) {
if(arr[i][j] > largestNumber) {
largestNumber = arr[i][j];

largeArr.push(largestNumber);
}
}

}

return largeArr[arr[i]];
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

``````

#6

Hello again Danny,

It looks like you’re having a little trouble with syntax and understanding what your code does. It’s really important for you to be able to read your code clearly. As you get better at understanding code you will write it much more proficiently and get better at doing awesome things with code. I could just give you the answer, but then you wont learn, and you’re here to learn, right? So lets talk about two different parts of your code and discuss what they do.

I’ll start us off as an example:

The first thing your function does when it is called is create a new variable `largeArr` and initializes it to an empty array (`[]`). Next, your code iterates (loops) over every index in `arr` from `i=0` to `i=1`, to `i=2`, … to `i=arr.length - 1`. The first thing your outer loop (the loop that uses `i`) does is initialize the variable `largestNumber` to the first (0th) element of the ith array in `arr`.

Ok, so what do you think about describing the rest of your code now using the description above as an example? What does your code do next? How can you describe what goes on in the nested for loop (the one that uses `j`)?, Can you describe what precisely your return statement does? It is a very good exercise to read your code carefully like this, you will learn a lot, I promise!

#7

for ( var i = 0; i < arr.length; i++ ) {
var largestNumber = arr[i][0];
for (var j = 1; j < arr[i].length; j++) {
if(arr[i][j] > largestNumber) {
largestNumber = arr[i][j];

largeArr.push(largestNumber);
}
}
}
return largeArr[arr[i]];
}

var largestNumber = arr[i][0] This creates a variable which is set to the value of the first number within the sub array j. ( first character zero indexed in the inner array )

for (var j = 1; j < arr[i].length; j++) This cycles through all the numbers in the inner array starting with the second number in each array ( Its is getting compared to the first number arr[i][0]

if(arr[i][j] > largestNumber) If the second number is larger than the first number in the arr it becomes the largest number and the comparison begins again with the new number eg. arr[i][1] against arr[i][2] and so on.

If there is a problem up to this point I am not sure what is happening

The next part largeArr.push(largestNumber); I only get in principal though I don’t get how it would keep cycling through. I think as it stands even if the rest of the code is correct this part would still only return you the higher of the comparison between the first and the second [0] and [1] with in the array.

This is best as I can explain it. happy to receive another text description understand if after that you want to give me the answer.

#8

var largestNumber = arr[i][0] This creates a variable which is set to the value of the first number within the sub array j. ( first character zero indexed in the inner array )

for (var j = 1; j < arr[i].length; j++) This cycles through all the numbers in the inner array starting with the second number in each array ( Its is getting compared to the first number arr[i][0]

Beautiful!

if(arr[i][j] > largestNumber) If the second number is larger than the first number in the arr it becomes the largest number and the comparison begins again with the new number eg. arr[i][1] against arr[i][2] and so on.

I think you understand this part, just to make it clearer I might word it as: “if the `j`th number in `arr[i]` is larger than `largestNumber`, then set `largestNumber` equal to the `j`th number”.

If there is a problem up to this point I am not sure what is happening

Nope, looks great so far! There’s a couple of issues below though.

The next part largeArr.push(largestNumber); I only get in principal though I don’t get how it would keep cycling through. I think as it stands even if the rest of the code is correct this part would still only return you the higher of the comparison between the first and the second [0] and [1] with in the array.

Awesome, there is an issue here, so lets talk about it. So `largeArr` starts as `[]`. `largeArr.push(1)` would make `largeArr = [1]`, and if you did `largeArr.push(-3)` after that, then largeArr would equal `[1, -3]`. Does that make sense? `largeArr.push(N)` adds `N` as the last element in `largeArr`.

You understand your code pretty clearly up to this point. Here’s one of the arrays from your sample input: `[32, 35, 37, 39]`, so lets suppose that we’re executing through your code. Currently, `largeArr=[]` and `i=0`. `arr[0] = [32, 35, 37, 39]`. With this in mind, can you read through the outer for loop. What does `largestNumber` equal at first? What does `largeArr` contain when `j=1`? what about when `j=2` and `j=3`?

Here’s probably a more important question: what do you want `largeArr` to contain?

Given your sample input, `arr=[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]`, what should `largeArr` contain at the very end of your function?

#9

Hi Josh

I understand the push function. Are you saying that the current code when looking at the first array [32,35, 37, 39] would push 35, 37, 39 in to the the largeArr because it would satisfy the condition as it cycled through?

largest number for array [32,35, 37, 39] would equal 32 first and then compare against 35 returning 35.

Though what we want to happen is for the code to cycle through the whole section of the sub array before returning a value hence only returning [5,27,39,1001]

There are only 4 j’s in each sub array so only three comparisons. Could I put in more steps for each cycle before pushing largest number? I know this is not elegant though I mention it here to lay out thinking

for ( var j = 1; j = 1)
if(arr[i][j] > largestNumber) {
largestNumber = arr[i][j];
for ( var j = 2; j =2)
if(arr[i][j] > largestNumber) {
largestNumber = arr[i][j];
for ( var j = 3; j = 3)
if(arr[i][j] > largestNumber) {
largestNumber = arr[i][j];

largeArr.push(largestNumber);

Thanks again for this!

#10

Hi Josh

I got passed the challenge doing the following

Rather than stages the push had to happen outside of the other stuff. I think this solved the problem of returning too many numbers. Then it was a case of just returning largeArr

I’d love it if you could could comment on this answer if you think I have missed any principals, especially if I’ve done the right thing for the wrong reason

``````function largestOfFour(arr) {
// You can do this!
var largeArr = [];

for ( var i = 0; i < arr.length; i++ ) {
var largestNumber = arr[i][0];
for (var j = 1; j < arr[i].length; j++) {
if(arr[i][j] > largestNumber) {
largestNumber = arr[i][j];

}
}largeArr.push(largestNumber);
}
return largeArr;
}
``````

#11

This looks great, Danny! Nice job working through the problem and explaining what you were thinking.

What’s also good about your final solution is that it will work for any positive number of elements in each of `arr`'s sub arrays unlike your solution one comment above. Think about what happens when `arr[i].length == 1`… the nested for loop won’t get executed at all, and the only number in `arr[i]` will get pushed to `largeArr`. Perfect!

I think your solution is clear and mintainable. Other than using `Math.max()` and styling your code so it is easier to read (spacing) I don’t think there’s any more principles to apply here.