Build the Largest Number Finder - Negative number declaration

Tell us what’s happening:

Hello,

I can’t find what i am supposed to do to make a declared variable let itself be a negative number.

Here, “biggest number” is assigned to 0. But it will encounter an array made entirely of negative number. It will then return 0 instead of the biggest negative number.

How am I supposed to declare the biggestNumber variable ?

Thanks a lot for your time and help!

Your code so far

let array = [[-72, -3, -17, -10], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];

function largestOfAll(arrs){
  let newArray = [];
  for (let array of arrs){
    let i = 0
    let biggestNumber = 0; //This number could be negative but won't be. Null is >-1 and undefined is undefined... D:
    for(let numberArr of array){
      if(numberArr >= biggestNumber){
        biggestNumber = numberArr;
      }
      i++;
      if (i >= array.length){
        newArray.push(biggestNumber);
      }

    }
  }
  return newArray;

}

let result = largestOfAll(array);

console.log("---------RANDOM TESTS---------")

console.log("----------Result----------");
console.log(result);


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0

Challenge Information:

Build the Largest Number Finder - Build the Largest Number Finder

Maybe try another approach using Math.max() or sort() to find the largest number in each array.

Thanks for the tips ! I’ll be able to do that with “sort” since Math.max looks awfull on array from what i see.

Still, is there a way to prepare a declared variable to be a number (that could be negative) ?

Math.max() - JavaScript | MDN

Not if you use spread syntax.

Damn, that was some cool things.

I learned about array.prototype.reduce() (it’s fantastic !)

also about using Math.max() that i totally forgot about !

also, you can have a “-Infinity” value for a negative number !

You rock. Thanks !

That’s great to hear!