# IF THEN ELSE with Negative Numbers - Basic Java

Tell us what’s happening:
Describe your issue in detail here.

Hello everyone. I have gone to the “hint” area and see that I nuked the ever living crap out of this. But I see how there is an easy way in the hints - but would someone PLEASE explain to me why this is giving me the console:

“Starting with highest number: -72
Looking at the next NEGATIVE number: -3
The current highest number is: -72
If -3 is < -72
Is -3 > ‘-72’?
NOPE!
-3 is NOT > ‘-72’
The highest number is still -72”

I do NOT get why it thinks that -3 is not larger than -72.

``````  **Your code so far**
``````
``````
function largestOfFour(arr) {
let num = [];

for (let i = 0; i < arr.length; i++){
console.log("-----------------------")
console.log("Round # "+i);
num.push();

for (let k = 0; k < arr[i].length; k++) {
console.log("----------")
console.log("Starting with highest number: " + num[i]);

// IF IT'S A NEGATIVE NUMBER

if (Math.sign(arr[i][k]) === -1)
{
console.log("Looking at the next NEGATIVE number: " + arr[i][k]);
console.log("The current highest number is: "+num[i]);
console.log("If " + arr[i][k] + " is < " + num[i])
if (arr[i][k] < num[i]) {
console.log("Is " + arr[i][k] + " < "+ + num[i])
num[i]=arr[i][k];
console.log("It is! (Negative)")
console.log("Highest number is now "+ num[i])

} else
{
console.log("Is " + arr[i][k] + " > "+ "'" + num[i]+"'?")
console.log("NOPE!");
console.log(arr[i][k] + " is NOT > "+ "'" + num[i]+"'")
console.log("The highest number is still "+num[i]);
}

}
//IF it's a POSITIVE NUMBER
else if (Math.sign(arr[i][k]) === 1)
{
console.log("Looking at the next POSITIVE number: " + arr[i][k]);
if(arr[i][k] > num[i])
{
console.log("Is " + arr[i][k] + " > " + num[i]+"?")
num[i]=arr[i][k];
console.log("It is!")
console.log("Highest number is now "+ "'" + num[i]+"'")
} else
{
console.log("Is " + arr[i][k] + " > "+ "'" + num[i]+"'?")
console.log("NOPE!");
console.log(arr[i][k] + " is NOT > "+ "'" + num[i]+"'")
console.log("The highest number is still "+num[i]);
}

}

}
console.log("The Largest numbers so far are: " + num);
}

console.log(num);
return num;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [-72, -3, -17, -10]]);
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36`

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

You seem to be turning yourself about with this positive/negative divide. You shouldn’t do anything different for positive or negative numbers.

I suspect what sent you down this path is this:

``````Starting with highest number: 0
``````

0 is a bad ‘guess’ for the maximum. You should instead ‘guess’ is one of the values you know is in the sub-array.

I got your code to work by cutting out the ‘NEGATIVE’ logic and changing one other line:

``````   num.push();
``````

There are two bugs with this line.

Side note though, all this `console.log` stuff is great! Exactly the sort of thing I do when debugging.

Jeremy,

So you just completely removed the whole section under my negative number check?

And thank you! I felt super “overboard” posting it here, but I was going crazy!

Well, that and tidied up braces so that only the positive logic was retained. Then I changed the initial ‘guess’ for what the highest number is. You can’t use 0 for this to work out right.

By the way, you can get a bit more readable output when logging the array by using:

``````console.log("The highest number is still " + num[i]);
``````

Jeremy,

I deleted the negative section and start out the “guess” as you put it with:

``````let k=0
num.push(arr[k]);
``````

now I get in the console for when there is a negative num

``````Round # 3
----------
Starting with highest number: 4
----------
Starting with highest number: 4
----------
Starting with highest number: 4
----------
Starting with highest number: 4
What am I messing up/not getting?
``````

`arr[k]` won’t work, but it is close. All of the entries in sub-array `3` are smaller than the first entry in sub-array `0`.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.