Return Largest Numbers in Arrays new day

so I’ve been working on this for two days now and it is supposed to be simple to do, so why am I still struggling.

A baby elephant was stuck in the mud. Mama couldn’t get him out 'cause she could not reach.
She saw a tree nearby and ran for it. She returned with a branch. That was longer than the pit of mud. The baby gladly grabs the branch and gets out to the safety of his Mama.

Guys, I need a branch.

Your code so far


function largestOfFour(arr) {
  // You can do this!
   var answer = [];
   //assigns an empty answer
   var i = 0
   /* assigns i to 0 for first position
   i took it out of the loop cause var in the loop confused me.*/
    for (; i < arr.length; i++){
    // this is the loop 
    var largestNumber = arr[i][0];
    // assigns arr to i and the sub array
    for (var sb = 1; sb < arr[i].length; sb++) {
      //another loop
      if (arr[i][sb] > largestNumber) {
        /* if both arrays or bigger than the largestNumber 
        than the largest number is than to be that number.
        So then my answer is 5 I don't understand.It shoulded be 5.*/
        largestNumber = arr[i][sb];
        answer = largestNumber; 
        
      }
    } return answer;
 
  
    
  }
}
 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 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0.

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

1 Like

Don’t worry, here comes the branch:

  • First of all, you aren’t adding largestNumber to the answer array. You are overwritting the array inside the if statement:
if (arr[i][sb] > largestNumber) {
  largestNumber = arr[i][sb];
  answer = largestNumber;  //This line is wrong, to add things to an array you use ".push(thing)"
}
  • The other thing that is wrong is where this line is located. I think that now that you know which lines is failing you can guess where is the right place to put it.

Good luck!

So I got it I just move things around changes so thing, until it finally worked.
all the ones that help I thank you. @ArielLeslie,@LoneWanderer,@camperextraordinaire, and @InternetFriend.
I can’t pinpoint any one person for the solution. So you all help me I took a little from each one of you. Thanks.

1 Like