Weird behaviour of javascript

V

Vmrincon

Hi everybody!

I am trying to program a function in javascript and a funny
behaviour occurs.
If I execute this code:

aux=parseInt(year-birthyear);

return (aux)

The value that the output returns is NaN (Not a number) but if I
execute this another one...

aux=parseInt(year-birthyear);
alert('hi')
return (aux)

Then the value that the output returns is right

It looks like javascript doesn´t have time to update the aux
variable to send it properly as an output... does anyone know how to
fix this problem?

Thanks a lot!

Victor
 
P

Pete

It looks like javascript doesn´t have time to update the aux
variable to send it properly as an output... does anyone know how to
fix this problem?

This shouldn't be the case.

try
parseInt(year)-parseInt(birthyear);
 
E

Erwin Moller

Vmrincon said:
Hi everybody!

I am trying to program a function in javascript and a funny
behaviour occurs.
If I execute this code:

aux=parseInt(year-birthyear);

return (aux)

The value that the output returns is NaN (Not a number) but if I
execute this another one...

aux=parseInt(year-birthyear);
alert('hi')
return (aux)

Then the value that the output returns is right

It looks like javascript doesn´t have time to update the aux
variable to send it properly as an output... does anyone know how to
fix this problem?

Thanks a lot!

Victor

Hi Victor,

To be honest: I am quite sure the problem lies somewhere else.
You real script is probably a lot more complicated, and you are just showing
us a piece of code.
I never heard of 'time needed to update a variable' in javascript or any
other time-dependant issues, unless you use interwindowcommunication with
callbackfunctions/loadtimes/ect.

The example you showed us does simple set a number or NaN in aux. And does
that before it executes the next line, be it alert or return.

Regards,
Erwin Moller
 
E

Erwin Moller

Pete said:
This shouldn't be the case.

try
parseInt(year)-parseInt(birthyear);

Very true, but do you believe that the alert fixes this somehow?

Regards,
Erwin Moller
 
V

Vmrincon

Very true, but do you believe that the alert fixes this somehow?

Regards,
Erwin Moller

I have also tried to implement a wait() function that will run a dummy
loop and with 3 seconds as a parameter, it makes the function work,
but the problem is that it slows down the application too much and
Internet Explorer shows a message (something like "A script is slowing
down your system..")

About the parseInt function I have tried and it does not make any
improvment....

Thanks a lot for your advices, but still can´t find a solution...
 
Y

Yanagita

Hi everybody!

I am trying to program a function in javascript and a funny
behaviour occurs.
If I execute this code:

aux=parseInt(year-birthyear);

return (aux)

The value that the output returns is NaN (Not a number) but if I
execute this another one...

aux=parseInt(year-birthyear);
alert('hi')
return (aux)

Then the value that the output returns is right

It looks like javascript doesn´t have time to update the aux
variable to send it properly as an output... does anyone know how to
fix this problem?

Thanks a lot!

Victor

Victor,
Did you try using the base? something like parseInt((year-birthyear),
10) ?
 
L

Lee

Vmrincon said:
Hi everybody!

I am trying to program a function in javascript and a funny
behaviour occurs.
If I execute this code:

aux=parseInt(year-birthyear);

If you execute that code, then it's hard to guess what
other mistake you might have made.

parseInt() accepts a string as its argument, not a number.
If you give it a number, it has to convert it to a string
before parsing out the integer value and returning it as
a number. Not very efficient.

To truncate a number to an integer, use:
Math.floor(year-birthyear);
or if you really want to round the number:
Math.round(year-birthyear);


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,426
Latest member
MrMet

Latest Threads

Top