freeCodeCamp Challenge Guide: Return Largest Numbers in Arrays

freeCodeCamp Challenge Guide: Return Largest Numbers in Arrays
0

I ended up stumbling on the Math.max() function when I was looking up methods to help with this problem, and I came up with this:

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

This is how I did it fellow campers:

function largestOfFour(arr) {
// You can do this!

var myArr = [];

var x = 0;
for(x = 0;x < arr.length;x++){
myArr.push(Math.max.apply(Math, arr[x]));

}

return myArr;
}

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

I have along way to go,seeing the declarative advanced and intermediate solutions , this is what i came up with.

function largestOfFour(arr) {
// You can do this!
longest = [];
for (i = 0;i<arr.length;i++){
arr[i].sort(function(a,b){
return b - a;
});
longest = [arr[0][0],arr[1][0],arr[2][0],arr[3][0]];
}
return longest;
}

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

I know its not the most efficient way but this is what i came up with i also found this site helpful https://gist.github.com/ourmaninamsterdam/1be9a5590c9cf4a0ab42#user-content-clone-an-array

i could have looped through instead. but i need more explanation on how i would loop. thats something i need to work on. i appreciate any feed back.

here is my code

function largestOfFour(arr) {

var indZer = arr[0].sort(function(a,b){
return b-a;
});

var indOne = arr[1].sort(function(a,b){
return b-a;
});

var indTwo = arr[2].sort(function(a,b){
return b-a;
});

var indThree = arr[3].sort(function(a,b){
return b-a;
});

var finalArr = [indZer[0], indOne[0], indTwo[0], indThree[0]];

return finalArr;
}

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

final answer [5,27,39,1001]

Hey everyone,

I came up with the solution using the sort() function - we did study it in one of the lessons. I hope it’s okay. Here’s my code:

function largestOfFour(arr) {

var largeArray = [];
var finalArray = [];

for (var i = 0; i < arr.length; i++) {
    largeArray[i] = arr[i].sort(function(a,b){
        return a - b;
    });
}

for (var j = 0; j < largeArray.length; j++){
    finalArray[j] = largeArray[j][3];
}

return finalArray;
}

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

Looking to these good answers of everyone, i feel me dumb, because my solution is the worst as possible :frowning:

function largestOfFour(arr) {
  // You can do this!
  arrai = [];
  
  arr1 = arr[0];
  arr1.sort();
  arr1.reverse();
  arr1 = arr1[0];
  
  
  arr2 = arr[1];
  arr2.sort();
  arr2.reverse();
  arr2 = arr2[0];
  
  arr3 = arr[2];
  arr3.sort();
  arr3.reverse();
  arr3 = arr3[0];
  
  arr4 = arr[3];
  arr4.sort(function (a, b){
    return a-b;
  });
  arr4.reverse();
  arr4 = arr4[0];
    
  arrai.push(arr1);
  arrai.push(arr2);
  arrai.push(arr3);
  arrai.push(arr4);

  return arrai;
}

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

hi there -

Here is my code, but I do not understand why it doesn’t work. I do not want to copy any response provided in this forum before really understanding it. Please help!!! Thank you so much!

function largestOfFour(arr) {
var largestNumbers = 0;
var newArr = [];

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

}

return newArr;
}

hi All - Sorry for the spam. After trying several times, I was able to resolve this challenge. I realized I had included a line of code in the wrong place.
QUESTION: SEE LINE OF CODE MARKED WITH QUESTION. WHY DOES THIS LINE NEED TO GO THERE? WHY CAN’T PLACE IT INSIDE THE INNER FOR LOOP?
THANKS A LOT FOR YOUR GUIDANCE!

function largestOfFour(arr) {
var largestNumbers = 0;
var newArr = [];

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

largestNumbers = newArr.push(largestNumbers); //I HAD THIS LINE INSIDE IF. **QUESTION**

}

return newArr;
}

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

I know this is not super well written but this is how my brain thought it out and it solved the issue at hand. I need to spend more time on .map() to make these kinds of things much simpler.

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

Yes I know the nested ifs are way out of hand lol

I’d like to contribute my slightly terser version of some of the other solutions here, because I hadn’t seen it.

    function largestOfFour(arr) {
return arr.map( (obj) => Math.max(...obj));
}

That said, I had read here that the beginning/elementary solution was the most performant. I like that the simplest tools in the language work best for problem solving of this sort and it’s awesome that we get to look at a variety of solutions. The main difference between my solution here and the other solutions that behave similarly is that I used the new spread notation and arrow functions to reduce the amount of code I needed to write. The downside is this would be totally opaque without a pretty thorough knowledge of the language. What time you save in writing this code would probably be lost in doing the necessary commenting.

2 Likes

That’s a really interesting thought pattern on this. You could make it a little more terse by storing the highest number in a variable and just iterating on the comparison. So like… compare two numbers, keep the winner, compare the winner with the next number, etc., until you’re all the way through the array

1 Like

Hello all~
I am very new at coding…and came up with the code below.
Hope that there is someone who would help me to take a look at my code…
I would be very grateful to know where I got wrong.

function largestOfFour(arr) {

for(var i=0 ; i<arr.length ; i++){
for (var j=0 ; j<arr[i].length; j++){
arr[i][j].sort(function (a,b){
return b-a;
});
}
}
return arr[i][0];
}

function largestOfFour(arr) {
var result = [];
var func = function(a, b) {
return Math.max(a, b);
};
for(var i=0;i<arr.length;i++){
var array = arr[i];
var max = array.reduce(func);
result[i] = max;
}
return result;
}

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

Hey guys,

I’m kinda new to JavaScript and coding in general, but this is the solution I came up with.

> function largestOfFour(arr) {
>   var newArr = [];
>   for (var i = 0; i < arr.length; i++){
>     arr[i].sort(function(a,b){
>       return a-b;});
>     newArr.push(arr[i][arr.length-1]);     
>     }
>   
>   return newArr;
> }

This is what I got, It’s messy but I feel really accomplished because this is the first algorithm I’ve been able to complete without consulting any extra resources or hints.

function largestOfFour(arr) {
  var arr1 = [];
    for (var d = 0; d < arr.length; d++) {
    var ord = arr[d].sort(function(a, b){return b-a});
    arr1.push(ord[0]);
  }

 return arr1;
}

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

I did it in a simple way. Sorted the array in descending order and then got the first element of each array.

function largestOfFour(arr) {
  var newArr = [];
  for(var i=0;i<arr.length;i++){
    newArr.push(arr[i].sort(function(a,b){
      return b-a;
    })[0]);
  }
  return newArr;
}

This is my solution. It works and uses the code we were taught up to this point. So my question is 'why can’t these pass the tests when they return the exact answers. The solution here is just as good and maybe even simpler than the ones offered as ‘official’.

my code :

var finalArr = [];
function largestOfFour(arr) {
  for (i = 0; i < arr.length; i++) {
      arr[i].sort(function sortNumber(a,b){
      return b - a;
    });
     
 finalArr.push(arr[i][0]);
  }
  return finalArr;
}

This is my approach to the given problem statement:-

function largestOfFour(arr) {
  let newArr = [];
  for(let i=0;i<arr.length;i++) {
    newArr.push(Math.max(...arr[i]));
  }
  console.log(newArr);
  return newArr;
}

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