PDA

View Full Version : need to know the syntax for setting up simple math functions in Java Script


G-MAX
09-05-2002, 07:21 AM
All of the documentation I've come across shows how to perform more complex math functions like Math.cos(x). What I need to do is multiply 2 variables together resulting in a 3rd variable.

Let's call our variables "%%mystore.shows.a.quantity%%", "%%mystore.shows.a.handling%%", and"%%mystore.shows.a.handling_total%%. So in essence, here's what I want to do:

%%mystore.shows.a.quantity%% *
%%mystore.shows.a.handling%% =
%%mystore.shows.a.handling_total%%

Let's say %%mystore.shows.a.quantity%% is equal to a value of "6".
Let's say %%mystore.shows.a.handling%% is equal to a value of "0.50".
I want the The resulting product value of "3" to be assigned to the variable %%mystore.shows.a.handling_total%%.

I'm not sure what's involved in writing this script and I could really use some help.

A few other things to consider:
1) The "quantity" and "handling" variables are already going to have values already assigned to them. The "handling_total" variable will be the result of their product
2) I DON'T want any of the variable values to be "manually entered in" with a button or field as in the <INPUT> command because they will already exist.
3) These variables are being used in the HTML <BODY> of an existing page.
4) These variables are being used in a table within an already existing <FORM>.

I would appreciate any help that anyone can give me. I'm not looking for someone to write the entire script for me. I just need a "push" in the right direction. This includes the proper script math commands and/or functions.

realisis
09-05-2002, 07:46 AM
G-MAX: I must be missing your point.... The method you seek is already pretty much contained in your post:

varC = varA * varB

or, to refer back to your variables, you need only state:

%%mystore.shows.a.handling_total%% =
%%mystore.shows.a.quantity%% * %%mystore.shows.a.handling%%

Btw, it's not clear what each of the substrings in your dot-syntax are intended to refer to: is "mystore" the window-name? Is it the form-name?

So, to bring it down to your actual form-fields... For purposes of this illustration, we'll take "mystore" to be the form-name... you can adjust as needed (I hope):

<SCRIPT>
<!--
calcFields = document.mystore.a

calcFields.handling.value = calcFields.handling_total.value * calcFields.quantity.value
//-->
</SCRIPT>

Put the script at the bottom of the BODY section (or at any rate, anywhere AFTER the form), and onLoad, your "handling" field should bear the approriate value (assuming the other two fields have values in them).

...

Or did I misunderstand you?

G-MAX
09-06-2002, 01:54 PM
Thanks realisis, that helps.

realisis
09-09-2002, 05:51 PM
ah, good going.

Cheers!