Reply
Newbie question - please help
Old 04-21-2005, 05:42 PM Newbie question - please help
Guerrilla's Avatar
Ultra Talker

Posts: 378
Hi,

I am learning javascript. Its the first language i have attempted to learn and i am finding it a bit tricky.

I am at the interactivity with forms part in my course so i have attempted to write a script with 3 input boxes. The goal is to have the first box be a single value, the second is double the first and the third is the square of the first.

I have been trying to get it to work for absolutely ages but its just not happening. Its probably glaringly obvious so i have copied the code below to see if any kind developer can help me out.

Here goes :

Code:
<html>
<head>
<title>3 numbers</title>
<script type="text/javascript">

function calculate(form,whichop) {

		if(whichop == "single") {
		form.double.value = form.single.value * 2;
		form.square.value = form.single.value * form.single.value;
		
		} else(whichop == "double") {
		form.single.value = form.double.value / 2;
		form.square.value = form.single.value * form.single.value;
										
		} else(whichop == "square") {
		form.single.value = Math.sqrt(form.square.value);
		form.double.value = form.square.value * 2; 
		
		}}
	

</script>
</head>

<body>


<form>

number <input type="text" name="single" value="0" onChange="calculate(this.form,'single')"> 
double <input type="text" name="double" value="0" onChange="calculate(this.form,'double')"> 
square <input type="text" name="square" value="0" onChange="calculate(this.form,'square')">

</form>

</body>
</html>
I appreciate any help you can give me.

Thanks
Guerrilla is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 04-21-2005, 06:46 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
I think the "else" statements in the function should be "elseif" instead. You can only have one "else" for an if statement, and you've got two.
__________________
Free Teacher Websites
Phaedrus is offline
Reply With Quote
View Public Profile
 
Old 04-23-2005, 07:16 AM
Guerrilla's Avatar
Ultra Talker

Posts: 378
i changed them to "else if" and it worked

Thanks for the help!
Guerrilla is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Newbie question - please help
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12719 seconds with 13 queries