Substring() error - how to get around it?

I hope this is the part of the code which is throwing an error.

var led = parseInt(led_msg.substring(4,5)) || 0;

How can I wrap this so if it doesn't exist that it gives a 0.

Hi Andrew .. maybe you can try to check if led_msg has a truthy value (if it exists)
before trying to substring or parseInt it.

var led = led_msg ? parseInt(led_msg.substring(4,5)) : 0

The ? and : is just a funcy way of writing if statements in one line
Which translates to

led_msg ? does led_msg have value ?
if yes do parseInt(led_msg.substring(4,5))
if not : set to 0

Thanks.

That magic is beyond me and my knowledge.

So if I put that line in, in place of the existing one, all should be ok?

yes .. replace it, fingers crossed that thats the problem ..
it does more or less the same with the only difference that it checks first if led_msg has a value.

Thanks.

That's fixed THAT problem. Now on to the ever growing list of others. :wink:

1 Like

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