Click to See Complete Forum and Search --> : eek @ my MATH function


Arrtu
01-10-2004, 06:38 PM
I am currently teaching myself javascript from a variety of sources, and I think I have got to grips with variables. What I am now trying to get on top of is variables and the math function. That is, using a form, I type in a number or character. I then type in a percentage into a second box and hit my go button. It returns a value defined in a variable which relates to the number I typed and calculates its percentage based on the second number I typed. So far so good. On running the script I return an error, which I assume is either my math or how I am trying to pass the value into my form:

<SCRIPT LANGUAGE="javascript">

function decide(){

var choice=document.form1.number.value;

if (choice<1)
document.form1.hundreds.value = 000;
if (choice==1)
document.form1.hundreds.value = 100;
if (choice==2)
document.form1.hundreds.value = 200;
if (choice==3)
document.form1.hundreds.value = 300;

var second=document.form1.percentage.value;
var show=math.round(hundreds / 100) * second;
document.form1.displaynumber.value=show
}

</SCRIPT>

<form name="form1">
number:<input type="text" size=10 name="number"><BR>
% multiple:<input type="text" size=10 name="percentage">
<INPUT TYPE="button" value="go" onClick="decide();"><P>
value:<input type="text" size=10 name="hundreds" readonly><BR>
percentage value:<input type="text" size=10 name="displaynumber" readonly>
</FORM>

As I said, I am teaching myself and I have tried various methods of resolving the issue, as I feel this is the best way to learn; but I now think it is time to ask an expert.
can anyone tell me where I have gone wrong, as I am going blind staring at my javascript bible? thanx in advance.

IsmAvatar
01-10-2004, 06:50 PM
1) what's the error?
2) yech, looks like you could use the switch and case things.

switch ( document.form1.number.value ) {
case 0: document.form1.hundreds.value = 000; break;
......
}

also, if it helps any, you can get the hundreds value of a number through this method:

floor(number / 100)*100

Arrtu
01-10-2004, 06:56 PM
Sorry, yes. Helps if I explain the error.

I type my number (eg 1) and then a percent (eg 25)

The return value is displayed as 100 in my third field (correct) but my fourth field which should return me a value of 25 (in the example I have used) remains blank and my status line at the bottom simply says "error on the page".

Thanx for this info I will give this a go.

ray326
01-10-2004, 07:15 PM
Math is capitalized: Math.round()

Arrtu
01-11-2004, 08:28 AM
Great. Got it working eventually.

Now for the next question. Let us imagine that I wanted to do a times table script, but instead of typing a number I selected it from a dropdown. So, I have two dropdown boxes with numbers 1-16, and selecting each number from the drop down passes a value to a textbox either onChange in each selection or onClick from a submit button
- how do I assign numbers from a dropdown list - do I have to parse them?

Then let us assume that I wanted to express the multiplied answer in a typed % - so in box 1 I have selected 15 and in box 2 I have selected 8 - it returns a value of 120

in a box below, I type 58, and in a fifth box it expresses 120 (brought forward from above) at 58%.

-to recap get the value of option1 and option 2 - multiply them and pass that number into a variable which is then displayed in box #3... enter a number to act as a per cent multiplier and express the total in box #3 multiplied by the per cent figure in a fifth box:

(diagram)

[select 1] x [select 2] = [box#3]

[input %] [express box 3 multiplied by input%]

Sorry to be a p.i.t.a. but Im just trying to think of really complicated functions to help me understand this math function as I am not picking it up too well from my javascript bible.

Khalid Ali
01-11-2004, 08:41 AM
this is how u can get a value from listbox
var listbox = document.formName.listBoxName;
var value = listbox.options[listbox.selectedIndex].value;

alert(value)

Arrtu
01-11-2004, 08:53 AM
excellent tvm. I have no idea what i would do without this place ;)