# Return Largest Numbers in Arrays help with output

## This has me confused

I have played with the output, used only 1 variable, and moved the return statement around, and no luck. I wanted to take a different approach, but i'm having trouble getting all the values saved to the new array. When i console.log the final output, i get the right answer, but only through each iteration.

``````function largestOfFour(arr) {
// You can do this!
var newArr =[];
var final = []
for(let i=0;i<arr.length;i++){
newArr =  arr[i].sort((a,b)=> a - b);
final = newArr.pop(arr[i][3]);

return console.log(final)
}

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

And a different result without the return

```<code>function largestOfFour(arr) {
// You can do this!
var newArr =[];
var final = []
for(let i=0;i<arr.length;i++){
newArr =  arr[i].sort((a,b)=> a - b);
final = newArr.pop(arr[i][3]);

console.log(final)
}

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

Try Math.max() function. Mdn

You are actually super close. letâ€™s take a look:

First off: That isnâ€™t how `console.log()` works.
`console.log()` is, in a way, a return statement itself. It just logs info to the console, so when you try to return it you will either get an error or simply `undefined`. So you are going to want to ditch it and just `return final`

Next, (and for some reason I see this a lot around this forum. Maybe there is an earlier lesson that needs to be clearer?) Most of the time a `return` statement does not want to be inside a for loop. The return will just stop the loop after one iteration (there are uses for this, that is why I said most of the time). If you want the loop to go to completion each time, you want to put the `return` after it.

And the `final` piece (see what I did there?) I see you using `Array.pop()` so Iâ€™m sure you learned `Array.push()` perhaps you may want to â€śpushâ€ť onto `final` the results of "pop"ing `newArr`?

Oh, and `Array.pop()` will automatically return the last entry, you donâ€™t need to pass `arr[i][3]` into it.

``````function largestOfFour(arr) {
// You can do this!
var newArr =[];
var final = []
for(let i=0;i<arr.length;i++){
newArr =  arr[i].sort((a,b)=> a - b);
final = newArr.pop(arr[i][3]);
console.log(typeof(final));
console.log(final)
}
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

console.log("=========================");

function largestOfFour(arr) {
// You can do this!
var newArr =[];
var final = []
for(let i=0;i<arr.length;i++){
newArr =  arr[i].sort((a,b)=> a - b);
final = newArr.pop(arr[i][3]);

console.log(typeof(final));
}
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
``````

Did you saw that I have added to check the typeof final which you declared as an arrayâ€¦??

It returned me as a type of â€śnumberâ€ť. You could better try some better way rather than â€śpopâ€ť method.

Your approach is really good. It personally made me to think. Happy Codingâ€¦!

Thank you for the help. I was trying to do something different than everyone elseâ€™s solution. I guess i got a little ahead of my self lol.

Than you very much for the guidance. I cant believe that i didn't think to push lol. But that's ok, because of great help like this, i know ill get it eventually.

1 Like

I think I my have been prematurely exited. i can get the right number each iteration, i just cant stick them 1 by one onto the same array. im stuck ATM.

Yup. and how would you stop the premature exit? (consider what return does and where you have that statement)

lol. thank you. this works.

```function largestOfFour(arr) { // You can do this! var newArr =[]; var final = []; for(var i=0;i<arr.length;i++){ newArr = arr[i].sort((a,b)=> a - b); final[i] = newArr.pop([i][3]);```

}

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

thanks so much. i moved the return statement before, i just quoted the old post lol.
1 Like

Trying different is good. Donâ€™t give up. Try more & more differently

does work, but it is kinda cumbersome. and like I said before, `newArr.pop()` is what you want not `newArr.pop([i][3])`. The stuff inside the parens is doing nothing.