Return Largest Numbers in Arrays --

Return Largest Numbers in Arrays --
0.0 0

#1

Tell us what’s happening:
Can someone help me figure out what is wrong with my code here.

Your code so far

function largestOfFour(arr) {    
var largeArray = arr.map(function(val){
      
      var b=0;      
      for(i=0; i<val.length; i++)
      {
        
        if(val[i]>b)
        {
          b = val[i];
        }
        else return b;
      }
});
  return largeArray;
}
      
 

  


largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
//, [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]```
**Your browser information:**

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

**Link to the challenge:**
https://www.freecodecamp.org/challenges/return-largest-numbers-in-arrays

#3

Hi @Fiasee

The problem is in your if/else logic. You’re only returning b if the last checked number in the array is bigger then the one you’re currently checking, which obviously won’t work if the largest number in the array your checking is the last element in that array, as is the case with the 3rd array in the example test. The result is an undefined entry in the mapped array as you didn’t return anything.

I suggest you re-evaluate the logic, you might find Math.max to be particularly helpful for this challenge.


#4

As said above the return, stops the function,
Since you want to return b, you have to return it after the for loop. This way you are sure all values have been tested :slight_smile:


#5

You can make your code work with 2 simple modifications ,
1st , you are initializing b with 0 instead of the first element of the sub array. Since the loop goes thru the array elements sequentially it is sufficient to initialize b with the first element.
2nd , as said above, you are exiting your for loop too soon, just wait till the loop is done and then return b.

function largestOfFour(arr) {    
var largeArray = arr.map(function(val){
      
      var b=val[0];      
      for(i=0; i<val.length; i++)
      {
        
        if(val[i]>b)
        {
          b = val[i];
        } 
      }
      return b;
});
  return largeArray;
}

#6

That shouldn’t be a issue, as it will change for the first element val[i]?


#7

What if val[i] is a negative number ?


#8

Yeah, I was just thinking that.


#9

Thank you all for the prompt replies, cleared the confusion now, really appreciate each of your replies.


#10

hi i am unsure why i am wrong here. I got the index of the largest array. But i still cant it run on the tests correctly. Even though on my chrone, when i console.log the output shows the biggest array out of the four

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


#11

I think you need the biggest number out of every array, not the biggest arrray in itself. "Return an array consisting of the largest number from each provided sub-array. "
So you need to loop through all the arrays to see what the biggest number is for every array inside the big array separately

[9, 6, 3, 4][4, 5, 6, 7] would return [9, 7] (the biggest one of the first one and the biggest one of the second one).

But i think you are almost there, you started well with 2 loops!