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;
}
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,
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.
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.