[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Datatype Conversion



Hello,

>From the JAVASCRIPT Guide:
-------------
Datatype Conversion

JavaScript is a loosely typed language. That means that you do not have to
specify the
datatype of a variable when you declare it, and datatypes are converted
automatically
as needed during the course of script execution. 

JavaScript will attempt to convert an expression to the datatype of the
left-hand
operand. Expressions are always evaluated from left to right, so JavaScript
applies this
rule at each step in the evaluation of a complex expression. 

For example, suppose you define the following variables 

var astring = "7"
var anumber = 42

Then consider the following statements: 

x = astring + anumber
y = anumber + astring

The first statement will convert anumber to a string value, because the
left-hand
operand, astring, is a string. The statement will then concatenate the two
strings, so x
will have a value of "742". 

Conversely, the second statement will convert astring to a numeric value,
because the
left-hand operand, anumber, is a number. The statement then adds the two
numbers,
so y will have a value of 49. 
-------------

Instead of 

form.total1.value = form.ent1.value + form.trav1.value + form.supp1.value +
form.car1.value + form.other1.value

try

form.total1.value = 0 + form.ent1.value + form.trav1.value + form.supp1.value +
form.car1.value + form.other1.value

At 18:57 10/01/96 -0500, you wrote:
>Hola,
>below is an expense report . . . I've got a little problem with the little
>javascript function "computeForm."  It keeps appending strings instead of
>adding them.  Any ideas?  I'd really appreciate any help.
>

Hope this helps.


--
Lourdes Yero
E-mail: lyero@dino.conicit.ve
PGP fingerprint: 77 B8 67 FE EC 5E 47 D9  62 94 4D D8 0C F8 F8 02