Return Largest Numbers in Arrays(Help question)

Alright, so I feel like I’m close but for some reason I’am unable to return the largest of the 4 sub-arrays. I feel like the placeholder value is incorrect or I am doing something wrong in the if statement. Any suggestions? Thanks!


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

console.log(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/84.0.4147.125 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Ok, so it won’t work for negative values, as 0 is bigger than negatives. So you can set the placeholder as the first value of every subarray.

This is how I tried to do that. But because the [i][j] loops are after the placeholder is declared globally I tried arr[0][0] which gives the same wrong array which is:
[ 5, 13, 27, 32, 35, 37, 39, 1000, 1001 ]

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

Declare the placeholder after the for loop that goes through every subarray.

Like this? It shows [ 5, 13, 27, 18, 26, 32, 35, 37, 39, 1000, 1001, 857 ]

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

Oh no. After where you loop through each subarrays, not the contents of the subarrays. So the i loop, not the j. Hope this helps.

Well I gave it a shot, if this isn’t what you meant I’ll probably figure it out if its just the positioning of where I put it lol

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

Ok, you want the placeholder to be the first value of every subarray. So placeholder[i][0].

After all that, while still inside the i loop, make the subarrays of newArr the placeholder. So newArr[i] = placeholder.

Then you should be good.

1 Like

Same as GhostRoboXt said.
You put placeholder at the wrong place. Move placeholder into sub-loop and find the largest number.

function largestOfFour(arr) {        
        let newArr=[];
        for (let i=0;i<arr.length;i++) {
            // Assume your array is not empty
            let placeholder=arr[i][0]; 
            for (let j=1;j<arr[i].length;j++) {
                if (arr[i][j]>placeholder) {
                    placeholder=arr[i][j];
                }
            }
            newArr.push(placeholder)
        }
        return newArr;
    }
1 Like

Thanks for the help! I passed with this

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

I put the placeholder value into the subloop and I saw that you moved the push statement. I moved the push statement and it passed. That is one thing I hate about programming is how much positioning matters. I feel like that is something I should have figured out on my own but I was assuming the push statement was in the right place the whole time. But thanks for pointing it out!

1 Like

It relates to the logic thinking. As you see, our old is starting from 0, 1 and then increase to 20 or older. Same as you put the code right position or not.

Just practicing more and you will find out that it is not so much difficult.

1 Like