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
|