HighDots Forums  

Weird behaviour of javascript

Javascript JavaScript language (comp.lang.javascript)


Discuss Weird behaviour of javascript in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Vmrincon
 
Posts: n/a

Default Weird behaviour of javascript - 05-29-2007 , 03:31 AM






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


Reply With Quote
  #2  
Old   
Pete
 
Posts: n/a

Default Re: Weird behaviour of javascript - 05-29-2007 , 04:05 AM






Quote:
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);




Reply With Quote
  #3  
Old   
Erwin Moller
 
Posts: n/a

Default Re: Weird behaviour of javascript - 05-29-2007 , 04:06 AM



Vmrincon wrote:

Quote:
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



Reply With Quote
  #4  
Old   
Erwin Moller
 
Posts: n/a

Default Re: Weird behaviour of javascript - 05-29-2007 , 04:07 AM



Pete wrote:

Quote:
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);
Very true, but do you believe that the alert fixes this somehow?

Regards,
Erwin Moller


Reply With Quote
  #5  
Old   
Vmrincon
 
Posts: n/a

Default Re: Weird behaviour of javascript - 05-29-2007 , 04:42 AM



On May 29, 11:07 am, Erwin Moller
<since_humans_read_this_I_am_spammed_too_m... (AT) spamyourself (DOT) com> wrote:
Quote:
Pete wrote:

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);

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...



Reply With Quote
  #6  
Old   
Yanagita
 
Posts: n/a

Default Re: Weird behaviour of javascript - 05-29-2007 , 08:24 AM



On May 29, 4:31 am, Vmrincon <vmrin... (AT) gmail (DOT) com> wrote:
Quote:
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) ?



Reply With Quote
  #7  
Old   
Lee
 
Posts: n/a

Default Re: Weird behaviour of javascript - 05-29-2007 , 12:44 PM



Vmrincon said:
Quote:
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);


--



Reply With Quote
  #8  
Old   
Pete
 
Posts: n/a

Default Re: Weird behaviour of javascript - 05-29-2007 , 11:09 PM




Quote:
aux=parseInt(year-birthyear);
How do you set the variables year and birthyear?



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.