Largest Number Array

Not given up on it (even though I’m totally lost lol). I would love some help with my code if possible. Many thanks guys.

  **Your code so far**

function largestOfFour(arr) {
for (let i = 0; i < arr.length; i++){
  for (let j = 0; j < arr[i].length; j++){
    let longestNumber = []
    if (arr[i][j] > longestNumber){
      longestNumber = arr[i][j]
return longestNumber;

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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

1 Like

thare is a lot to fix, i’ll give you 3 suggestions:

  • you can’t declare with let a variable inside a loop and next call it outside
    (how you did for let longestNumber = [])
  • You have to return an array, so you have to push items inside of it:
    you did:
    longestNumber = arr[i][j]
    you should do:
  • in your if statement there is:
    arr[i][j] > longestNumber
    but you can’t compare a number with an array, you can do something like:
    arr[i][j] > longestNumber[0]

next you should try to understand the logic…
happy coding!

1 Like

Oh my word! Thanks. Great pointers, truly appreciated.


Why not use a for…of loop to solve the problem?

1 Like

I do actually understand the logic of your suggestions (I tried .push but it came up as an error). Thank you.

1 Like

Because that’ll be easier to read and confuse you less!

1 Like

I see! Many thanks for that.

1 Like

And your longestNumber array is being reset every time.

1 Like

Were you able to figure out the solution?

So, the question is asking you to find the largest numbers, n-u-m-b-e-r-S in the 4 arrays. Not the largest. But the largest in each array.
max(arr1, arr2, arr3, arr4)
So, you have to loop through the 2d array and go through each array and find the largest item in it and add it to the list of largest numbers.
And at the end return the largest numbers array.

1 Like

I’m with you. I’m going to hit the hay and work on it tomorrow. This has helped a great deal! Many thanks @gururajankappa!

1 Like

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