Hi again.
I have a small problem, and was hoping someone could point out where I'm going wrong.
(It's worth mentioning that Math isn't a strongpoint, and that may be the root of my problem!)
Below is a block of code for calculating the average score for a block of inputs, but I need it to reduce the number averaged by, if there is an N/A in any of the selection fields.
For some reason, I get minus numbers, so I'm obviously doing something wrong... just can't for the life of me see what.
(Is it even valid code to have if statements inside if statements???)
In advance, many many thanks for taking the time to look at this for me.
-Dufty
code:
<html>
<head>
<script LANGUAGE = JavaScript>
function calculateTotals(x) {
with (document.forms["myForm"])
{
var totalScore = 0;
var divIde = 0;
if (x == 1) {
divIde = 3
if (element1.value == "0" ) {
divIde = -1
}
if (element2.value.value == "0") {
divIde = -1
}
if (element3.value == "0") {
divIde = -1
}
totalScore = Number(element1.value) + Number(element2.value) + Number(element3.value);
blockAvg1.value = avgCalc(totalScore/divIde,2);
}
function avgCalc(num,pow) {
num *=Math.pow(10,pow);
num = (Math.round(num)/Math.pow(10,pow)) + "";
return num;
}
}
}
</script>
</head>
<body>
<form name=myForm>
<table>
<tr valign="top">
<td> Calculate Average</td>
<td> <input type="button" value="Block Average" onClick=calculateTotals(1)>
<input name="blockAvg1" type="text" value="?"> </td>
</tr>
<tr valign="top">
<td>
<div align="left">Element 1</div></td>
<td> <div align="left">
<select name="element1">
<option value="0" selected>N/A</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</td>
</tr>
<tr valign="top">
<td> <div align="left">Element 2</div></td>
<td> <div align="left">
<select name="element2">
<option value="0" selected>N/A</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</td>
</tr>
<tr valign="top">
<td><div align="left">Element 3</div></td>
<td> <div align="left">
<select name="element3">
<option value="0" selected>N/A</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
WOW! Just posting it here revealed one problem:
code:
if (element1.value == "0" ) {
divIde = -1
}
should read:
if (element1.value == "0" ) {
divIde = divIde -1
}
But still inaccurate....
[This message has been edited by Dufty (edited 10-24-2002).]