Negative numbers in the arrays

Tell us what’s happening:
Im working on this challenge of selecting the largest number in a set of arrays, but Im facing the problem of negative numbers. it does not read them.

Your code so far
function largestOfFour(arr){
var largestArray = ;
for(var i = 0; i<arr.length; i++){
var largestNumber = 0;
for(var j = 0; j<arr.length; j++){
if(largestNumber < arr[i][j]){
largestNumber = arr[i][j];
}
}
largestArray.push(largestNumber);
}
return largestArray;
}


function largestOfFour(arr){
var largestArray = [];
for(var i = 0; i<arr.length; i++){
   var largestNumber = 0;
   for(var j = 0; j<arr.length; j++){
      if(largestNumber < arr[i][j]){
         largestNumber = arr[i][j];
      }
   }
   largestArray.push(largestNumber);
}
return largestArray;
}

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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Your initial guess of the max value is too big.

 var largestNumber = 0;

You should try a better initial guess, maybe a value that you know is in the array?

1 Like

I just initialize it to 1, and the array containing only negative number return 1, I think the solution is to make sure that the value you initiate involves all the numbers,

Right. The problem is that this value

 var largestNumber = 0;

could be larger than anything in your array. You don’t want to use a fixed value as your initial guess. You should use the array to help you out.

Great, how can I use a dynamic guess?. because there is no need of every time setting that value, it far better have a universal guess that can deal with positive and negative number at the same time.

If I have a bag of slips of paper with numbers on them and I’m told to find the biggest number, the first thing I will do is pull out a slip and read the number. That number would be a good guess for the largest number in the bag.

Yeah, I got it, I feel it would be better to have a guess which can represent all the numbers!. Anyway thanks!.

There is no single number that would work because you can always find an array of numbers that has values smaller than whatever number you guess : )

Hi! This one stumped me for a bit, too, but I think I’ve got a solution for you!

Like others have said, you’re automatically disallowing negative numbers from the array by assigning 0 to var largestNumber. Instead of assigning it a value of 0 or declaring it as a new Number (which also doesn’t work), instead try using:
Number.NEGATIVE_INFINITY;

This will effectively allow your code to accept any number, positive or negative, and compare their values appropriately.

Read more about it here: https://devdocs.io/javascript/global_objects/number/negative_infinity

P.S. While this does work, there is another, cleaner solution by studying the arrays. So for anyone else who needs help, I would try for that.

1 Like

That will work, but I’m not super enthusiastic about that solution.

let largestNumber = arr[i][0];
for (let j = 1; j < arr[i].length; j++)
  largestNumber = Math.max(largestNumber, arr[i][j]);

This is so much cleaner to me.

1 Like

This work exactly as I wanted it to work!. Thanks!.

1 Like