parseInt() sorry, but I seem to be missing something

With all the recent stuff I've been doing, asking and told I would have thought I would have got this down pat by now.

Seems not.

Code extract:

let d = msg.delay;
d = parseInt(d / 10000);
if (d > 10)
{
    d = d / 60
}

node.status({text:msg.payload + " " + d});
return msg;

Of interest: d.

So is d an integer or not?

Why?
This is what I see below the node:

4.4333333333333333333333 doesn't qualify to me as an integer. :confused: :wink:

Maybe msg.delay was 2600000 to start with. So you get to the if with d == 260. 260/60 is 4.333333
parseint is a function. Not a type. You have to call it whenever you need it.

1 Like

Ok, I guess the final /60 could be throwing a spanner in the works.

Sorry.
And thanks.

Adding some background here:

The docu says: The parseInt() function parses a string argument and returns an integer.

There's an additional note:

Note: JavaScript does not have the distinction of "floating point numbers" and "integers" on the language level. parseInt() and parseFloat() only differ in their parsing behavior, but not necessarily their return values. For example, parseInt("42") and parseFloat("42") would return the same value: a Number 42.

Thus you're a lucky person that JavaScript is really forgiving & parseInt(d / 10000) works at all. Behind the scenes, it's doing several (!) type conversions to process your request:

Pseudo code:

d => cast to number
calc d/10000 => result, typeof number
result => cast to string
perform parseInt(result) => d, typeof number

d may look like an integer, yet as Javascript has no integer type, it's still of type number.
Thus d/60 is a division of type number ... creating a result of type number.

1 Like

Thanks.

But as pointed out to me by @dceejay the latter /60 was throwing things into getting that weird number I showed on the screen shot.

I have since changed the code to:

d = d / 1000;
if (d > 10)
{
    d = parseInt(d / 60);
}

Works fine now. :slight_smile:

Sure it does. :wink:
Being a bit picky here, it's yet quite 'wrong'.
d is not a string, it's already of type number.
To round to an integer-like value, Math.round() or Math.floor() are your friends here...

1 Like

Ok. No problems.

The problem - this end - is the lack of my knowledge of all the commands.

I'll go off and see if I can get my head around those commands.

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