freeCodeCamp Challenge Guide: Return Largest Numbers in Arrays

Hello,

I just used this code and not sure why i have to use the

var results = [];

in the beginning, and how that links to the

results[i] = firstNumber;

return results;

portion of this. Could someone explain for me?

thanks

Allen

1 Like

Never mind, i get it, it’s because I have to return my own array of the top 4 numbers.

1 Like

Thanks for this!

It’s along the path I took to solve the problem, but slightly more efficient than mine. I mapped, then sorted the array before using my for statement. How did you know you can place the “[0]” at the end of the sort method? It makes sense, but I haven’t seen it in the tutorials so far.

Here’s mine

function largestOfFour(arr) {
var results = [];
arr.map(function(a) {
a.sort(function(a,b) {
return b - a;
});
});

for (i = 0; i < arr.length; i++) {
results.push(arr[i].shift());
}
return results;
}

1 Like

Hey @vsmarcuslewis,

thanks for that reply, it is always good to here nice words.
Since we are talking about it, your approach is pretty neat and very readable.

To answer your question:
I still knew that you could access sub-arrays by adding another [n] behind the first one, but I am not sure if this was explained on FCC, as I use several resources to learn programming.
Anyway I used that and put in the “0” to always use .push on the first element of the sorted sub-arrays.
This would save an additional step (or a loop inside another loop).

If you have further questions please let me know :slight_smile:

1 Like

Thanks Jan.
Yea I just started using FCC, trying to do 2 hours a day of studying I’ve had a hard time in the past focusing on actual studies. I noticed I should try to crack open a few books I’ve bought over the years to actually read through to learn more efficiently. FCC is helping though.

I’ll keep chugging through and see how far I can get!

Thanks again :slight_smile:

After going through the Basic Algorithm, I’m definitely noticing I’ll need to start looking at other sources along with FCC to progress. A lot of the solutions needed are using functions and elements that weren’t taught in the Front End Dev Cert section.

So much to learn, so much to figure out!

2 Likes

Yeah that’s true and basically what Quincy Larsson (the creator of FCC) says.
I would suggest to look into the “get job ready course”.

1 Like

Here is what I have come up with: (I’m just over a week into coding)

function largestOfFour(arr) {
// You can do this!
var sorted = [];
var largest = [];

function sortNumber(a, b){
return a-b;
}

for (var i=0; i < arr.length; i++) {
sorted.push(arr[i].sort(sortNumber));

}
for (var j=0; j < sorted.length; j++) {
largest.push(sorted[j].pop());
}

return largest;

}

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

3 Likes

And here I go:

function largestOfFour(arr) {
  
  //array for storing largest numbers
  var largestArr = [];
  var largest = 0;//create largest number to have something to compare to
  //Loop through an array 
  for(var i=0; i< arr.length; i++){
    //loop through an indicidual numbers of each array
    for(var y=0; y< arr[i].length; y++){
      //compare each number to our largest number declared earlier
      if(arr[i][y] > largest){
        largest = arr[i][y];
      }
      
    }
    
    largestArr.push(largest);//add largest number to an array
    largest = 0;//reset largest number for the next loop phase
  }
  return largestArr;
}

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

I don’t know about you guys, but those solutions in spoiler section are a bit complicate. Maybe bc I’m new to programming. Anyway after a couple of hours ;-), with breaks I came with my own algorithm:

function largestOfFour(arr) {
   
  var newArr = [];    
  for(var i = 0; i < arr.length; i++){
      
     newArr[i] = Math.max.apply(null, arr[i]);
    
  } 
  
  return newArr;
}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);
5 Likes

I did it with different way, Can anyone comment my mistakes;

    function largestOfFour(arr) {
        // You can do this!
          var arr1 = []; 
        for (var i = 0; i < arr.length; i++) {
              arr1.push(arr[i].sort(function(x,y) {return y - x;}));
                 }
            arr1 = [arr1[0][0],arr1[1][0],arr1[2][0],arr1[3][0]];
                   // console.log(arr1);
                     return arr1;
            }

     largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);

`

1 Like

My solution:

function largestOfFour(arr) {
  var largestNumbers = [0, 0, 0, 0];
  for (i = 0; i < arr.length; i++) {
    for (j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > largestNumbers[i]){
        largestNumbers[i] = arr[i][j];
      }
    }
  }
  return largestNumbers;
}

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

Up to this point I’ve been struggling with coming up with some “slick” code compared to the hints.But in this case I think I’ve leaped ahead. This compact solution will work for any number of arrays and any number values in a sub-array:


function largestOfFour(arr) {
  if (Array.isArray(arr)) {
    subArray = [];
    for (i = 0; i < arr.length; i++) {    
      subArray.push(Math.max.apply(null, arr[i])) ;
    }
    return subArray;
  }
  return "Not an Array";
}

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

Here is the solution that I came up with:

function largestOfFour(arr) {
// You can do this!
var result = [];
var max = 0;
for(var i=0;i<arr.length;i++) {
for(var j=0;j<arr.length;j++) {
if(arr[i][j]>max) {
max = arr[i][j];
}

}
result.push(max);
max = 0;

}
return result;
}

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

1 Like

function largestOfFour(arr) {
// You can do this!
var max=0;
var array=[];
for(var i=0;i<arr.length;i++)
{
for(var j=0;j<arr[i].length;j++)
{
if(arr[i][j]>max)
max=arr[i][j];
}
array[i]=max;
max=0;
}
arr=array;
return arr;
}

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

1 Like

I agree with you Naib…I came up with the same solution.

3 Likes

Hello,

I am wondering what is wrong with this code, it does not pass on the following array of arrays:

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

It returns [5,27,39,1001] instead.

My code is following:

Code

Thank you for your help in advance.

Brg

1 Like

I’ve seen a lot of similar solutions but I thought I’d post mine for comparison:

function largestOfFour(array) {
var arr = []; //sub-array
var ans = [];// array of answers
for(i = 0; i < array.length; ++i)
{
arr = array[i];
/.push adds data to end of ans, .sort sorts the data(small - big) then gives the
last element (the biggest number).
/
ans.push(arr.sort(function(b, a){return a < b;})[3]); //
}
return ans;
}

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

1 Like

function largestOfFour(arr) {
// You can do this!
var newArray = [];
for(var i=0;i<arr.length;i++){
for(var y=0;y<arr[i].length;y++){
if(newArray[i]==undefined){
newArray[i] = arr[i][y];
} else if(newArray[i]<arr[i][y]){
newArray[i] = arr[i][y];
}
}
}
return newArray;
}

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

1 Like

Seem to have found a quick elegant way - found something similar to the basic solution and when trying to recreate the intermediate solution this seemed to work for everything:

function largestofFour(arr) {
arr.reduce(function(a,b) {
return b-a;
});
}

1 Like