PDA

View Full Version : Code Debugging needed!


Antoniohawk
09-24-2002, 01:55 AM
Can someone plz debug this code?

<html>
<head>
<style>
INPUT {
FONT-FAMILY: Verdana, Helvetica;
FONT-SIZE: 10px;
border: 1px green solid;
background-color: #000000;
color: green;
}
</style>
</head>
<body>
<script language=javascript>
function calc(f){
var num1 = f.fire1.value;
var num2 = f.fire2.value;
var num3 = f.fire3.value;
if (isNaN(num1)){
alert("Number 1 must be numeric.");
f.fire1.focus();
return;
}
else if (isNaN(num2)){
alert("Number 2 must be numeric.");
f.fire2.focus();
return;
}
else if (isNaN(num3)){
alert("Number 3 must be numeric.");
f.fire3.focus();
return;
}
else if (num3=="0"){
alert("Number 3 must not be zero.");
f.fire3.focus();
return;
}
f.fireoutput.value = (num1-num2)/num3;
}
</script>

<script language=javascript>
function calcwood(f){
var num1 = f.wood1.value;
var num2 = f.wood2.value;
var num3 = f.wood3.value;
if (isNaN(num1)){
alert("Number 1 must be numeric.");
f.fire1.focus();
return;
}
else if (isNaN(num2)){
alert("Number 2 must be numeric.");
f.fire2.focus();
return;
}

f.woodoutput.value = (num1-num2)/num3;
}
</script>






<table cellspacing="2" cellpadding="0">
<tr>
<td bgcolor="gray" style="font-size:15px; color: white; padding-left: 10px; padding-right: 10px; padding-top: 10px; border:1px #000000 solid">
<form name="form">
Enter in the experience you would like: <input name="fire1">
<br>
Enter in your firemaking experience: <input name="fire2">
<br>
Enter in how much exp. you get per fire: <input name="fire3">
<center><input type="button" value="Submit" onclick="calc(this.form)"></center>
<br>
</td>
</tr>
<tr>
<td bgcolor="gray" style="font-size:15px; color: white; padding-left: 10px; padding-right: 10px; padding-top: 10px; border:1px #000000 solid">
You have to make <input name="fireoutput"> fires!

</form>
</td>
</tr>
</table>

<br>
<br>

<table cellspacing="2" cellpadding="0">
<tr>
<td bgcolor="gray" style="font-size:15px; color: white; padding-left: 10px; padding-right: 10px; padding-top: 10px; border:1px #000000 solid">
<form name="form">
Enter in the experience you would like: <input name="wood1">
<br>
Enter in your woodcutting experience: <input name="wood2">
<br>
<center><input type="button" value="Submit" onclick="calcwood(this.form)"></center>
<br>
</td>
</tr>
<tr>
<td bgcolor="gray" style="font-size:15px; color: white; padding-left: 10px; padding-right: 10px; padding-top: 10px; border:1px #000000 solid">
You have to cut <input name="woodoutput"> regular trees!
<!--the answer to the equation is outputted here--//>
</form>
</td>
</tr>
</table>

</body>
</html>

The first form is working great, but i want alot of those form s for different purposes on one page. Can anyone help me out here?

glenngv
09-24-2002, 02:28 AM
you can make the function generic like this:


function calc(in1,in2,in3,out){
if (isNaN(in1.value)){
alert("Number 1 must be numeric.");
in1.focus();
return;
}
else if (isNaN(in2.value)){
alert("Number 2 must be numeric.");
in2.focus();
return;
}
else if (isNaN(in3.value)){
alert("Number 3 must be numeric.");
in3.focus();
return;
}
else if (in3.value=="0"){
alert("Number 3 must not be zero.");
in3.focus();
return;
}
out.value = (num1-num2)/num3;
}
</script>

<form name="f1">
...
<input type="button" value="Submit" onclick="calc(this.form.fire1,this.form.fire2,this.form.fire3,this.form.fireoutput)">
</form>
<form name="f2">
...
<input type="button" value="Submit" onclick="calc(this.form.wood1,this.form.wood2,this.form.wood3,this.form.woodoutput)">
</form>


as i said, don't name your form as form and name your forms differently.

Antoniohawk
09-24-2002, 09:30 PM
thx again glenn