Hi everyone. So i’ve been trying to solve this particular question and i just can’t move forward, i don’t know how to think through it. I tried using double four loops but it can only find one element in the array, i don’t know how to think this through, can someone explain it to me please

**Your code so far**

function largestOfFour(arr) {
let longest = [];
for (let i = 0; i < arr.length; i++) {
for ( let j = 0; j < arr[i].length; j++) {
if ( )
}
}
return arr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

**Your browser information:**

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

How would you describe it in words if this was a logic problem instead of a code problem?

For example, if I handed you several stacks of playing cards and I said “I want you to hand me back the highest card from each stack.” how would you do it?

This may seem silly, but a lot of times we can lose track of the logic by getting caught up in the code too quickly.

Probably you will need a new variable inside the loop to work with the values, then compare the values, is this number greater than this? and save the true values on that variable.

Sorry if you can’t understand this way, is hard for me to explain without giving you the solution

You do not say what kind of output you are looking for, so I am trying to guess it by your code. Are you looking to produce an array where each of its elements represent the max value of each of the four arrays passed to the function? Or are you looking for the max integer value of all integers passed to the function? Since your function is returning an array, I’m guessing the first assumption is the correct one.

If so, you only need one loop, not two. Since you said you are not looking for the solution, I am just going to give you a hint.

function largestOfFour(arr) {
let longest = [];
for (let i = 0; i < arr.length; i++) {
let elem = max(arr[i]);
longest.push(elem);
}
return longest;
}

Now, all you need to do is creating the max function and have it return the max value for each array passed to it. I just did it and everything worked fine. Cheers.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

your solution looks fine to me, and if you prefer to keep it, without using asgaard’s ‘max()’ alternative, i used 2 temporary variables declared within the first loop and the return array variable declared before the loops.
click on spoiler below if you want example (or don’t, it’s half the final code)

function largestOfFour(arr) {
let big = []
for (let i=0;i<arr.length;i++){
let sub=arr[i]
let temp=sub[0]

Try think of the challenge into smaller chunks. First figure out a way to find the largest element in an array. Then you loop thru the main array, find largest element in every sub array and push it to the result you must return. So far you have a loop that go thru the main array and you have a loop which goes thru every sub array. Now you need to make the second loop find the largest number in the respective array and once its complete, push that number to the result array.

Hi, i’ve finally solved the question but not with this method, but i would like you to solve it completely so i can understand how it works please, thank you for your reply.