<font color="#ff0000" size=5>WebTV JS "Bug" - Reload Page (cmd-r)</font>
Doing JS Calculations


Calculations in JS can be coded in the body of a document with little problem. A variable is defined and depending on how it is written it is either a string or a numeric variable. Mathematical operations on each, it has been seen, are performed diffferently. Consider the following three representations:

Addsum=x+y
Concatenateunite="x"+"y"
Determineresult="x+y"


If variables x and y were defined as numbers a sum would be given. Concatenation would not be done, nor would the expression be determined. Concatination joins independent strings into a longer string. The expression, as it is now is just a string variable.

The Eval FunctionBuilt into JS is an "eval" function which can take a string as an argument. The string can be a mathematical expression, a JS statement, or a sequence of statements. Included in the string can be predefined or existing JS variables or properties. Upon using the string as an argument in the "eval" function all mathematical expressions or program statements are performed, e.g.,

<script>
var total=0;
total=eval("21.23+32.74");
document.write(total)
</script>
   Displayed


Setting the variable "total" to 0 is an initialization. It can be seen now that mathematical expressions can be written out as a string variable and by use of the eval function they will be converted to numerics and all operations performed. This means text input of this kind can be entered using forms and the results displayed in any way desired.

Forms: Input & Output To use forms as input and/or output the form used must be given a name, as must each input-tag, e.g.,


<script>
function domath(obj)
{obj.IDOUTPUT.value=eval(obj.IDINPUT.value);}
</script>
<form name="IDFORM">
<input type="text" name="IDINPUT" size=20>
<input type="number" name="IDOUTPUT" size=20>
<input type="button" value="Result" onClick=domath(this.form)>
<input type="reset" value="Clear"><form>


The text-input windows below are operational examples:

Enter Math Expression:


Result (click to get):


Only expressions with +, -, *, or ÷ can be used here. Try some: 12*2/3, (10-2)/4*(6-4). Any others which include math functions will not evaluate, e.g., sqrt(3) gives "undefined" and exp(+5)*exp(-2) gives "NaN", meaning "not a number". This is a limitation of the input string to numeric conversion and not of the eval function itself.

The form contains all information needed for calculations, so it is a user-defined object, "obj". The elements used on the form are the ones named in the inputs, "IDINPUT" and "IDOUTPUT". For the JS evaluation the string from the input window, named "IDINPUT", has all things specified:

obj.INPUT.value


It is contained in the user defined object, a form, and the string is the "IDINPUT.value". A similar identification is done to direct where the result is to go: within the user defined object and having the "IDOUTPUT.value". The result is displayed "onClick" from the JS function for the user defined object, "this.form". It should be noted that the input-type to generate the form window is specified as "number". A numeric is to be displayed, and not text.

Parsing There two built-in functions which convert strings to numerics. One, parseFloat, attempts to convert a string argument to a floating-point number. If the argument is other than a number the result is NaN. A useful means of testing to see if there is an input typing error. The other, parseInt, can have a floating-point number for an argument and truncates, i.e., drops off, all characters that are non-numeric. The result is an integer. If the first character in the string cannot be converted to a number the return is NaN.

parseFloat( )

parseInt( )



Try these. Enter a number and then "15 March" for the parseFloat trial and a floating-point number and then "15 March" with the parseInt trial.


JS Test-Bed 1 - - JS Test-Bed 2



<< PreviousNext >>