JS error in function

Questions and information about using VBScript and JavaScript in NeoBook functions

Moderator: Neosoft Support

Locked
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

JS error in function

Post by dpayer »

I am attempting a JavaScript function that converts from decimal to hexidecimal and back.

I've found the appropriate methods and here is the function (actual function file below):
(here is an example I'm working from and I have also verified the methods at w3schools.com)
[syntax=JavaScript]if ([%1] = 'decnum') {

newhexnum = [%2].toString(16);
nbSetVar ( '[newhexnum]', newhexnum );
}
if ([%1] = 'hexnum') {

newdecnum = (parseInt([%2], 16)) ;
nbSetVar ( '[newdecnum]', newdecnum );
}[/syntax]

Two parameters: [%1] is the type of number being submitted use either decnum or hexnum
[%2] is the number submitted
Script should generate a NB variable populated with the appropriately converted number.

I've created a sample app (below) to submit the variables to the function. It works fine for decimal to hex conversion. I'm having problems with the opposite.
When converting from Hex to Dec this happens:
It says it is expecting a ';' (semicolon) on the line it displays but it has one. I've tried adding a space to no avail.
(what I really don't understand is why it even enacts this section if the conditional IF fails, which it does)
Image

Can someone see my error?

App code: create a new 320x200 app and paste this in:

Code: Select all

{NeoBook 5 Objects}
NeoBookVer=5.80
ObjectType=9
Name=TextEntry1
X=110
Y=27
W=192
H=34
Anchor=0
VarName=[DecVal]
Align=1
EditNumber=Yes
EditLen=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=2
ObjectType=8
Name=Text1
X=10
Y=29
W=86
H=32
Anchor=0
Font=Arial
FontSize=16
FontStyle=0
FontCharset=1
TextColor=0
Text={\rtf1\ansi\deff0\deftab254{\fonttbl{\f0\fnil\fcharset1 Arial;}}{\pard{\qr\li0\fi0\ri0\sb0\sl\sa0 \plain\f0\fs32\cf0 Decimal}}}
HMargin=0
VMargin=0
LineColor=0
LineWidth=0
LineStyle=0
FillColor=16777215
FillPattern=1
TabOrder=3
ObjectType=8
Name=Text2
X=12
Y=90
W=86
H=32
Anchor=0
Font=Arial
FontSize=16
FontStyle=0
FontCharset=1
TextColor=0
Text={\rtf1\ansi\deff0\deftab254{\fonttbl{\f0\fnil\fcharset1 Arial;}}{\pard{\qr\li0\fi0\ri0\sb0\sl\sa0 \plain\f0\fs32\cf0 Hex}}}
HMargin=0
VMargin=0
LineColor=0
LineWidth=0
LineStyle=0
FillColor=16777215
FillPattern=1
TabOrder=4
ObjectType=9
Name=TextEntry2
X=111
Y=84
W=192
H=34
Anchor=0
VarName=[HexVal]
Align=1
EditChars=1234567890abcdefABCDEF
EditLen=0
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=5
ObjectType=13
Name=RadioButton1
X=116
Y=131
W=76
H=25
Anchor=0
Text=Hex2Dec
Align=1
VarName=[RadioGroup1]
InitState=0
ObjAction=SetVar "[decval]" ""
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=6
ObjectType=13
Name=RadioButton2
X=116
Y=152
W=76
H=25
Anchor=0
Text=Dec2Hex
Align=1
VarName=[RadioGroup1]
InitState=0
ObjAction=SetVar "[hexval]" ""
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=7
ObjectType=3
Name=PushButton1
X=219
Y=134
W=89
H=42
Anchor=0
Text=Convert
Align=2
ImageStyle=0
XPTheme=Yes
ObjAction=If "[RadioGroup1]" "=" "Hex2Dec"¶Call "convertHexDec" "hexnum" "[HexVal]"¶AlertBox "Decimal Equivalent" "The decimal equivalent of [hexval] is|[newdecnum]"¶Endif¶If "[RadioGroup1]" "=" "Dec2Hex"¶Call "convertHexDec" "decnum" "[DecVal]"¶AlertBox "Hex Equivalent" "The hexadecimal equivalent of [decval] is|[NewHexNum]"¶Endif
LineColor=0
LineWidth=1
LineStyle=0
FillColor=16777215
FillPattern=1
FillMode=Yes
Font=Arial
FontSize=10
FontStyle=0
FontCharset=1
TextColor=0
TabOrder=8
You will need this function file named convertHexDex in your function folder

Code: Select all

{NeoBook Function}
Version=5.80
Language=JScript
Comment=Describe number being submitted as hexnum (hexadecimal) or decnum (decimal). Submit number to convert. |If hexnum ---> returns NB variable [newdecnum]|If decnum ---> returns NB variable [newhexnum]
Param=[%1]|Text|Format of number being submitted. Use: hexnum or decnum
Param=[%2]|Number|Number being submitted for conversion
{End}
if ([%1] = 'decnum') {

newhexnum = [%2].toString(16);
nbSetVar ( '[newhexnum]', newhexnum );
}
if ([%1] = 'hexnum') {

newdecnum = (parseInt([%2], 16)) ;
nbSetVar ( '[newdecnum]', newdecnum );
}

David Payer
Des Moines, Iowa
USA
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: JS error in function

Post by Gaev »

David Payer:

Never mind the confusing error message.

The problem arises because in your Function definition, [%2] was defined a Number ... so when you pass a string value for Hexadecimal (like 'aa'), it balks.

I changed the definition of second parameter to "Text" ... and then this code worked ...

Code: Select all

if ([%1] = 'decnum') {
    thisNumStr = '[%2]';
    thisDec = parseInt(thisNumStr,10);
    newhexnum = thisDec.toString(16);
    nbSetVar ( '[newhexnum]', newhexnum );
}
if ([%1] = 'hexnum') {
    thisHexStr = '[%2]';
    newdecnum = (parseInt(thisHexStr, 16)) ;
    nbSetVar ( '[newdecnum]', newdecnum );
}
You may be able to shorten it a bit ... but I was too lazy to try ... "if it aint broken, don't try to fix it"
(what I really don't understand is why it even enacts this section if the conditional IF fails, which it does)
I think it is associated with the weird way Javascript figures out the "type" of a variable ... during its "Just In Time Compilation" process ... heard cases of variable "Scopes" (global/local) not being assigned as expected as well ... something to do with it parsing the entire function to first determine the "Type" ... and commands not participating in the logic messing up "Scopes" because of it.
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: JS error in function

Post by dpayer »

Thanks Gaev,

I had originally used text as the 2nd parameter but when I attempted to convert it to a number, I got an error.

But I used this function: thisDec = Number([%2]) and it didn't work as I expected.

You used thisDec = parseInt(thisNumStr,10); and I understand how it works.

I appreciate your helping us all with your coding knowledge.
David Payer
Des Moines, Iowa
USA
User avatar
dpayer
Posts: 1394
Joined: Mon Apr 11, 2005 5:55 am
Location: Iowa - USA

Re: JS error in function

Post by dpayer »

Thanks Gaev,

I had originally used text as the 2nd parameter but when I attempted to convert it to a number, I got an error.

But I used this function: thisDec = Number([%2]) and it didn't work as I expected.

You used thisDec = parseInt(thisNumStr,10); and I understand how it works.

I appreciate your helping us all with your coding knowledge.
David Payer
Des Moines, Iowa
USA
User avatar
Gaev
Posts: 3782
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada
Contact:

Re: JS error in function

Post by Gaev »

David Payer:
I had originally used text as the 2nd parameter but when I attempted to convert it to a number, I got an error.
But I used this function: thisDec = Number([%2]) and it didn't work as I expected.
I don't know why it did not work.

This page has a button to try out the function ... or you can go directly to the practice page

Could it be that you were passing a value that contained alphabetic characters (like spaces or commas) ? ... in which case it would return NaN ... which might fail later on in the code ... you might try inserting ...

Code: Select all

thisDec = Number([%2]);
alert("..." + [%2] + "..." + thisDec);
... to investigate further.
Locked