# Return Largest Numbers in Arrays-applying for loop

Hello all,
i tried this challenge by sort() method and its working. But the code is getting lengthy. Can someone help me applying a for loop in my code?

``````function largestOfFour(arr) {

let ans =[]
let elem1 = arr.sort(function(a,b){
return b-a})
ans.push(elem1);

let elem2 = arr.sort(function(a,b){
return b-a})
ans.push(elem2);

let elem3 = arr.sort(function(a,b){
return b-a})
ans.push(elem3);

let elem4 = arr.sort(function(a,b){
return b-a})
ans.push(elem4)
``````

Take a look back at one of the previous lessons.

If you notice you basically have the same thing written four times. Try and look over this lesson and see if you can use that code one time rather then four inside a loop, using the loop to select portions of the array.

1 Like

thanks! i think i got it now. i knew i needed to apply a for loop but i was not sure where to because it had a subarray. thanks for your suggestion. here is the code now

function largestOfFour(arr) {

let ans =
for(var i = 0; i < arr.length; i++){
let elem = arr[i].sort(function(a,b){
return b-a})
ans.push(elem);}
return ans

Just as a hint, you don’t need to sort anything here. Each subarray is just a list of numbers, and Javascript provides a math method for getting the max from a list of numbers.

1 Like

i think the Math.max() does not works with an array of numbers

Always a good idea to look at the documentation rather than assuming:

1 Like

ohn yes. you are right, it works but with a spread operator. when i was trying it, i forgot to add it. as i am a beginner i make a lots of mistakes. But thanks for your correction. appreciate it. thanks. 1 Like

this function can be written in one line of code. One option to achieve that:
to use reduce(), spread operator and Math.max.

1 Like

Yep, same for everyone!

The “spread” operator in this context is pretty simple and really useful in a few situations – it just says “if you have an array, take the elements and pass them as arguments to the function” (works with objects as well). There aren’t that many functions where this is actually useful (that take any number of arguments of the same type of thing), but `Math.max` (and `min`) do.

For this challenge, it makes things clearer. I can see there’s a loop over an array. If I can then see `Math.max`, I know those subarrays are collections of numbers, and that you’re getting the max number from each collection. With `sort`, I just know those sub arrays are lists of {something} and you’re then ordering them {tries to remember which way is ascending, which descending…} and you’re then getting the {largest? smallest?}. That makes it much less likely that you introduce errors as well

Edit: you don’t need to be clever about it either, loops are really clear, don’t need to try to get into one line (though that is very easy here once you’re familiar with array methods).

1 Like

yes you are right. but i am still learning and dont know much about reduce(). i will try to emprove. thanks for your suggestion:)

1 Like

Oh, there will be material about it in next sections of curriculum.

1 Like

function largestOfFour(arr) {
let ans =
for(var i = 0; i < arr.length; i++){

let elem = Math.max(…arr[i])

ans.push(elem);}
return ans }

Thank you for these advices, i will keep those in mind for future coading experiences. 1 Like

Just to dump something else on you (it’ll also be introduced later, so ignore it for now if you want), there’s another type of loop statement that tends to make things clearer as well if you’re looping over an array (or a string, or anything) – `for...of`. Instead of having to keep track of an index (like `for (let index = 0; index < arr.length; index++) ` then using `arr[index]` in the loop), it gives you the element instead (like `for (let element of arr)`).

So equivalent to your solution (I hope this makes sense…):

``````function largestOfFour(numArrs) {
let largestNums = [];

for (let numArr of numArrs) {
let currentLargest = Math.max(...numArr);
largestNums.push(currentLargest);
}

return largestNums;
}
``````
1 Like

after reading your post, i tried the for…of loop in other challenges, and it works like magic , , saves time , and the code looks more clean. Thanks again for your suggestions.
and can you please explain why do we have For loop,(or where to use the for loop specifically) if the For…of loop can do a better job?

A `for` loop just says “do (something) over and over”. So it’s extremely flexible.
A `for...of` loop says “iterate through the entries of a collection of things” (more specifically, something that is an `Iterable`, that is set up to be iterated through in order – an array, a string, a set, a map)
yes. and the for loop can run in both direction (forward and reverse) unlike the for…of loop, Thanks for your help to make it clear 