I'm stuck and I don't want to look at the solution, I want to understand the question

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

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

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.

2 Likes

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

1 Like

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

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]
1 Like

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.

2 Likes

Perhaps a different approach might help.

Can you write a function to find and return the largest value in a simple array of numbers? That piece is going to be useful in a minute.

Once you have that function working, can you think of a way to apply it repeatedly, say to an array of arrays?

Try building small parts first, and building them to bigger things.

1 Like

Hi, thank you so much, i used the max function and it solved the problem.

Hi, thank you very much, yea this method of thinking was what i needed.

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.

Hi, thanks, this is my first time asking a question in the forum.

Hi, thanks i used the Max method and it worked, thanks for your response.

Hi, thank you so much, reading your reply helped me think about it properly, i finally solved it.

Congratulations on working through it! Happy coding.

1 Like

you probably used a better method but i wanted to get familiar with a nested loop.

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

      for(let j=1;j<=sub.length;j++){
        if(sub[j]>temp){temp=sub[j]}
      }
    big[i]=temp
  }
  return big
}

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

Hi, thank you very much for this solution, i realize that i need to learn how t use Temporary variables and nested loops extensively.

be warned, you best avoid temp variables if possible. you are better using max().
just good to know nested loops.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.