# If in an If doesn't work

**URL:** <https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028>\
**Category:** General\
**Created:** [24 July 2023 02:50 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028 "2023-07-24T02:50:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nice\_to\_have](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nice\_to\_have](https://discourse.nodered.org/u/Nice_to_have)\
**Post date:** [24 July 2023 02:50 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028/1 "2023-07-24T02:50:58Z")

</div>

Hi everyone,

I want to check if a number is negative or positive.  
But untill now, `global.set(port + ".NMEA.nav.position.alt.altitude_relative"` is not written

```auto
if (string[0] == "GGA") 
{
    global.set(port + ".NMEA.UTC.time", string[1]);
    global.set(port + ".NMEA.nav.position.dms.lat", string[2]);
    global.set(port + ".NMEA.nav.position.dms.NS", string[3]);
    global.set(port + ".NMEA.nav.position.dms.lon", string[4]);
    global.set(port + ".NMEA.nav.position.dms.EW", string[5]);
    global.set(port + ".NMEA.nav.fix.fix", string[6]);
    global.set(port + ".NMEA.sat.sat_used", (string[7]*1));
    global.set(port + ".NMEA.nav.position.accuracy.HDOP", string[8] * 1);
    global.set(port + ".NMEA.nav.position.alt.altitude", (string[9]*1));
    global.set(port + ".NMEA.nav.position.alt.altitude_unit", string[10]);
    global.set(port + ".NMEA.nav.position.alt.altitude_ellipsoid", (string[11]*1));
        if (Math.sign(string[11] === -1)) 
        {
        global.set(port + ".NMEA.nav.position.alt.altitude_relative", ((string[11] - string[9])*-1));
        }
            else if (Math.sign(string[11] === 1)) 
            {
            global.set(port + ".NMEA.nav.position.alt.altitude_relative", ((string[11] - string[9]) * 1));
            }
    global.set(port + ".NMEA.nav.DGPS.update", string[12]);
    global.set(port + ".NMEA.nav.DGPS.age", (string[13]*1));
    global.set(port + ".NMEA.nav.DGPS.id", string[14]);
} 

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 July 2023 05:09 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028/2 "2023-07-24T05:09:45Z")

</div>

I'm not sure but....

```auto
if (Math.sign(string[11] === -1)) 
        {
        global.set(port + ".NMEA.nav.position.alt.altitude_relative", ((string[11] - string[9])*-1));

```

I'm not sure what `altitude_ellipsoid` mean as you have an `altitude` already.  
So reading it how I see it:

`if` look at the `Math.sign` of `string[11]` and see if it is `-1`  
(IF TRUE)  
`((string[11] - string[9])*-1))`

So let's look at this:

`[11]` is say..... `-5`  
`[9]` is 20

-5 - 20 \* -1  
= -25 \* -1  
= 25

Isn't the _extra_ `* -1` at the end negating things?

`-` \* `-` = `+`

Why are you having the last bit (on two lines - though different) of `*-1` and `* 1`?  
To me they aren't needed.

But I have been wrong before and will be again.

But to me that is _wrong_. Somehow.

---

<div class="post-metadata">

**Author:** ![Nice\_to\_have](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nice\_to\_have](https://discourse.nodered.org/u/Nice_to_have)\
**Post date:** [24 July 2023 05:32 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028/3 "2023-07-24T05:32:19Z")

</div>

Hi Trying\_to\_learn,

the difference between the `*-1` and `* 1` is that it removes the negative.  
If `string[11]` is negative, do calc and make it positve.  
if `string[11]` is positive, do calc and let it positive.

But I am not sure why this calculation doesn't work at all.  
The result is that `global.set(port + ".NMEA.nav.position.alt.altitude_relative"` is not written to global.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [24 July 2023 05:37 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028/4 "2023-07-24T05:37:40Z")

</div>

In case of `string[11]` is 0, there will be no action.

Try to trace down actual values.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 July 2023 05:53 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028/5 "2023-07-24T05:53:30Z")

</div>

That's a good catch @hotNipi  
I didn't even think of that one.

---

<div class="post-metadata">

**Author:** ![Nice\_to\_have](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nice\_to\_have](https://discourse.nodered.org/u/Nice_to_have)\
**Post date:** [24 July 2023 13:23 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028/6 "2023-07-24T13:23:39Z")

</div>

My bad, I misunderstood what Math.sign did. 🤦‍♂️

> Blockquote  
> The **`Math.sign()`** static method returns 1 or -1, indicating the sign of the number passed as argument. If the input is 0 or -0, it will be returned as-is.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [24 July 2023 14:01 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028/7 "2023-07-24T14:01:43Z")

</div>

> [@Nice\_to\_have](#):
>
> `if (Math.sign(string[11] === -1))`

That syntax is also wrong I think. It should be:

```auto
if (Math.sign(string[11]) === -1)

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 September 2023 14:02 UTC](https://discourse.nodered.org/t/if-in-an-if-doesnt-work/80028/8 "2023-09-22T14:02:08Z")

</div>

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