Return Largest Numbers in Arrays 5641

Tell us what’s happening:

hello i am make a condition work for the negate number cases. i am stuck on how to make th
at work, the following code only works for the positive numbered cases. Any help will be appreciated …
thanks
Your code so far


function largestOfFour(arr) 
{
  // You can do this!
      let arrLargestNum = [];
    for (let go in arr) 
    {
        let testNum = 0;
        for (let goSecondArr in arr[go]) 
        {

            if (arr[go][goSecondArr] > testNum) 
            {
                testNum = arr[go][goSecondArr];

            }


        }
        arrLargestNum[go] = testNum;
        //console.log(arr[go][goSecondArr]);
    }
    
    //return arr;

  return arrLargestNum;
}

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 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0.

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

You have to make testNum = first value in the array (if any), and start the loop from the second value.

hey
thanks for your response but the loop counter goes over the array length and won’t work anymore

Hi there benibishi,

All you have to do is add another if statement in this for loop:

Before you go into your if statement, add another if statement and check if the number is below 0, if so, just invert the number to positive and you’ll have your answer.
Something like:

arr[go][goSecondArr] = -arr[go][goSecondArr];

thanks for your response
That doesn’t make sense. if you make the negative numbers to positive then the biggest negative number would be chosen as the biggest number in the last array. it wont work. that won’t work

I honestly thought this was what you meant.

If thats the case, then you don’t need to change your code, negative numbers (bigger than the positive) will be automatically be deemed as lower than any positive number.

It shouldn’t go over the max length if your ending condition is correct. Here’s what I mean (adapt it to your code):

let largest = arr[0];
for (let i = 1; i < arr.length; i++) {
  if (arr[i] > largest) largest = arr[i];
}

i understand what you mean now. so where do you put this value in the code. let largest = arr[0]. for any reason the variable largest is positive the condition wont get executed. i am trying to pass the last test case given on freedom codecamp. you have a test case that as positive and negative numbered array in nested. this test case to be more clear
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

My
let largest = arr[0];
would be something like your
let testNum = arr[go][0];

(In my example it’s just searching the largest number of one array. But you could use the same idea to search the largest of several arrays)

ok ok that makes sense. thanks for the response
ok ok i got that. that makes perfect sense. there are two for foops and i had to initilize the testNum like this (let testNum = arr[go][0]) on the first for loop and do if conditions ON THE SECOND LOOP to compare the numbers with the first element.

ok thanks man that makes so much sense

Yes you got that right. Well done :+1: