Return Largest Number in Arrays (subarray query)

Tell us what’s happening:

I realize the hint was to go for a simple loop, but I thought there might be another way to go about it. Also, I’m having difficulty understanding the logic of a loop in this case…

I am attempting to organize the the sub-arrays in descending order so that I can call forth the first element of each sub-array and that would be the largest one. 1)Is this the best way to go about it? 2)If so, where am I going wrong?

The console says that I am not defining i, but I shouldn’t need to because i is the array of arrays…or should I? As you can see, I’m a bit confused.

Your code so far


function largestOfFour(arr) {
//create empty array
var result = [];
//organize elements into descending order
var arr1 = arr[i][j];
var arr2 = arr1.sort(function(a,b){
  return (b-a);
});
//call forth first element of each subarray
result = arr1[j][0];
return result;
}

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




//keep tracks of arrays highest values in another array

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/

Hi!

The idea is good, but you have to apply sort function to all sub-arrays. I don’t understand what are you trying to do with var arr1 = arr[i][j];, and like console says, the i and j variables are not declared…

But anyway, I don’t see where you want to go with that. You need a loop or something or something similar to order each element (array) of the main array.

Tell us what’s happening:

I wrote a loop, but I’m still a bit lost on
1.Is my loop pulling out what I want from arr? (It’s not but is it close?)
and
2. Can I .push() the result from the above effort into my empty array?

Are these questions moot as I haven’t done the necessary groundwork?
If this is the case what aspects am I missing to successfully complete the groundwork?

Your code so far


function largestOfFour(arr) {
//create empty array
var result = [];
var i = "";
var j = "";
//organize elements into desending order
for(var i=0;i<4;i++) {
  for(var j=0;j<4;j++){
    arr.sort(function(a,b){
      return(b-a);
    })
  }
}
//call forth first element of each subarray
result = arr[j].push();
return result;
}

What do want to achieve with this line?

Why have you defined them?

Even this nested loop does not make sense.

Your approach to the problem is correct.

With this line you are sorting the arr 4 times.
You should sort the subarrays instead.

See if this helps.

Well, I am returning an array now which is something but I don’t seem to be passing any of the other tests.

This is what I have. I’ve worked with the loop and sort relationship but nothing seems to have come to fruition.
Where am I messing up?


function largestOfFour(arr) {
  // You can do this!
  var i;
  var j;
  var result = [];
  for(i=0;arr.length>i;i++) {
    var highest = 0;
    for(j=0;arr[i].length>j;j++){
      arr[i].sort(function(a,b) {
        return b-a;
      })
      highest = arr[i].length-1;
    }
    result.push(highest);
  }
  return result;
}

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

I would suggest using Math.max() for finding largest number from list of numbers, a method created specifically for this and not hacking your way around :slight_smile:

I don’t understand why this is only passing the first test. I think that I’ve gone through the process pretty meticulously. Where have I messed up?


function largestOfFour(arr) {
  //create empty array
  var result = [];
  //declare variables
  var i;
  var j;
  //loop through outer array to determine number of subarrays
  for(i=0;i<arr.length;i++){
    //store determination from outer loop
    var biggest = arr[i][0];
    //loop through subarrays to find biggest number
    for(j=0;j<arr[i][0].length;j++){
    //compare result from outer array to result from inner array
      if(arr[j] > biggest){
        //assign variable from outer array to result from inner array
        biggest = arr[j];
      }
    }
    //push final result onto empty array and return it
   result.push(biggest); 
  }
  return result;
}

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

Tell us what’s happening:

I don’t know how else to proceed.

I am trying math.max() but that doesn’t seem to be working. When I console.log() the result of it I get only the first subarray. I tried sort(), imagining that I could select the largest number through bracket notation, but sort() just copied arr as well as the sorted arr into a larger array.

I think I am right on the edge of getting it, but I am a little frustrated that I can’t figure out the correct method because I think I am skipping over an important method that would help to solve this problem easily, Please help.

Your code so far


function largestOfFour(arr) {
  //create empty array
  var result = [];
  //declare variables
  var i;
  var j;
  //loop through outer array to determine number of subarrays
  for(i=0;i<arr.length;i++){
    //store determination from outer loop
    var max = arr[i];
    console.log(max);
  for(j=1;j>max[i].length;j++) {
    if(arr[i][j]> max) {
?????
    }
  }
  }
   
  return result;
}

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

Okay. I figured it out, but I really want to know why this worked. I don’t want to give anything away, but I’ll just say that I thought that considered how many arrays there were that you would have to do two loops but I didn’t have to do this. Why didn’t I have to do two loops and then assimilate these together into a single []. Can someone explain this to me?

I posted a link to my solution below. Can someone explain to me why this worked without more?

I have to ask, did you actually come up with the solution yourself? Because it doesn’t sound like you did. Maybe don’t use a solution if you don’t understand it.

Do you know what this does? Can you explain how apply works, and why it is used?
Math.max.apply(Math, arr[i])

By the way, you still have the j variable, but you are not using it.

I looked on a lot of message boards for inspiration of what other people were doing to give me a nudge in some sort of trajectory. I saw one that used Math as a parameter and as I was using arr[i] and getting nothing I plugged it in to see what would happen.

That said, I have been using j variable unsuccessfully.

I am still puzzling over how to use var j so that I can thread the values gleaned from i loop. I am puzzled by why this works and would like to know why. This doesn’t mean that I’ve given up solving the solution for myself though…

Well i just mean if you’re not using it, and you don’t need it, you should remove it (the j variable).

The answer to why it works is in the question i asked you, look at how apply works and see if you can come to a conclusion.