Closed Thread Icon

Topic awaiting preservation: I think I am right, but not work..... (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8662" title="Pages that link to Topic awaiting preservation: I think I am right, but not work..... (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: I think I am right, but not work..... <span class="small">(Page 1 of 1)</span>\

 
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-19-2003 23:59

Hi, guys. How are you today? Would you please see my code???

My mission is:

Get a user to enter the three dimensions of a box. Output the volume, the surface area and the total length of all the edges of the box.

Here is my code:

code:
<html>
<head>
<script type="text/javascript">
function cal()
{
document.theForm.text4.value=document.theForm.text1.value*document.theForm.text2.value*document.t

heForm.text3.value
document.theForm.text5.value=2(document.theForm.text1.value*document.theForm.text2.value +

document.theForm.text2.value*document.theForm.text3.value +

document.theForm.text3.value*document.theForm.text1.value)
document.theForm.text6.value=4(document.theForm.text1.value+document.theForm.text2.value+document

.theForm.text3.value)
}


</script>
</head>
<body>
<form name="theForm">
<p>Input the heigth:
<input type="text" name="text1" size="3"></p>
<p>Input the width:
<input type="text" name="text2" size="3"></p>
<p>Input the length:
<Input type="text" name="text3" size="3"></p>
<input type="button" value="Submit" onClick="cal()">
<input type="reset" value="Reset">
<p>This object's volume:
<input type="text" name="text4" size="5"></p>
<p>This object's area:
<input type="text" name="text5" size="5"></p>
<p>This object's length:
<input type="text" name="text6" size="5"></p>
</form>
</body>
</html>



I thought my code should work. But only volume can be shown on the text box. Area and length doens't work......

And actually I am tired of writing like document.theForm.text1.value etc..... Would you know how to avoid this untidy repeatidity???

Many many thanks for sharing your time.
Cya.

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-20-2003 09:14

Well, the reason it's not working is that you're using this strange "2(...)" notation. Perhaps you mean "2*(...)"? In any case, the browser should be giving you an error message. In IE, for instance, check the lower left of the browser and the status bar should say there's an error message. Double click on that to get information about the error. Be sure to click "previous" until you get to the first error. I also recommend checking the checkbox that makes IE alert you about errors right when they happen.

quote:
And actually I am tired of writing like document.theForm.text1.value etc..... Would you know how to avoid this untidy repeatidity???



Make a few temporary variables:

var text1 = document.theForm.text1.value;
var text2 = document.theForm.text2.value;

etc, then just use those variables, which are copies of the values of the textboxes.

rickindy
Nervous Wreck (II) Inmate

From: Indianapolis, In USA
Insane since: Jan 2002

posted posted 05-20-2003 13:43

I've found Mozilla to be very helpful when debugging javascript. It's Javasctip console tool will show you exactly where the error is. I won't leave home without it.
www.mozilla.org



Few problems in life can't be solved by chocolate

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-20-2003 23:04

Hi, Slime. Hi, rickindy!
Many many thanks for your advices.
It was great helps.
I really appreciate them.

Well, IE5 pointed out where my error was like Slime said to me. It pointed out same problem which Slime told me. It was great discovery.

But I am afraid I have a wee one more question.

Is there any difference between below. I thought it meant exactly same. But as you know it is being calculated totally differenct answers.

code:
document.theForm.text6.value=4*(document.theForm.text1.value+document.theForm.text2.value+document.theForm.text3.value)

document.theForm.text6.value=4*document.theForm.text1.value+4*document.theForm.text2.value+4*document.theForm.text3.value



Many many thanks.
Have a happy Wednesday.

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 05-22-2003 09:55

Mathematically, those two statements are equivalent. However, the values of text inputs are not numbers, they're *strings* (that is, if you type in "44", the text box doesn't contain the number 44, it contains the string consisting of two '4' characters). So JavaScript isn't doing *math* in the first one - it's appending the strings together. It only starts doing math when it seeds the multiplication operator (*), since it can't do multiplication with strings. In the second statement you gave, it does math right away, because it sees the multiplication operator right away.

To force it to see the values as numbers instead of strings, use the parseInt() function (to get an integer from a string) and the parseFloat() function (to get a fractional number from a string). The following two statements should be 100% equivalent:

code:
document.theForm.text6.value=4*(parseFloat(document.theForm.text1.value)+parseFloat(document.theForm.text2.value)+parseFloat(document.theForm.text3.value))
document.theForm.text6.value=4*parseFloat(document.theForm.text1.value)+4*parseFloat(document.theForm.text2.value)+4*parseFloat(document.theForm.text3.value)

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-22-2003 11:38

Hi, Slime. Many many thanks for your reply.
I got it.
I am going to have a can of beer now.
It makes me happy before going to bed.
Cya.

Hiroki Kozai

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 05-22-2003 23:25

Hi, slime.
parseFloat!
It is cool!
Many thanks.
I am excited!!!

Hiroki Kozai

« BackwardsOnwards »

Show Forum Drop Down Menu