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.
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:
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
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.
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]]);
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.