# Need Help With Calculation

Discussion in 'Javascript' started by pmarisole, Aug 11, 2006.

1. ### pmarisoleGuest

I need help in calculating a score from a row of drop-down values.
I need to use the onChange to tally the score as the user moves across
9 categories (with drop-down selection of 1-9 or N/A) multiplied by a
weighted score. The final column for each employee would be the
calculated score. I know how to accomplish this with vbscript but I
need it to process on the screen so the user can see the calculated
score as they make selections. Also the code needs to execute as many
times as there are employee rows.(loop) I don't know how to do this
with Javascript

Here's the VBscript with the variables involved
Var S1 = Category 1 Weighted score
Var S2 = Category 2 Weighted score
Var S3 = Category 3 Weighted score
Var S4 = Category 4 Weighted score
Var S5 = Category 5 Weighted score
Var S6 = Category 6 Weighted score
Var S7 = Category 7 Weighted score
Var S8 = Category 8 Weighted score
Var S9 = Category 9 Weighted score
D5 = 0.92
Var Q11 = Category 1 drop-down Value(1-9) * S1
Var Q22 = Category 2 drop-down Value(1-9) * S2
Var Q33 = Category 3 drop-down Value(1-9) * S3
Var Q44 = Category 4 drop-down Value(1-9) * S4

If (Category 5 Value = "N/A") THEN
Q55 = ' '
Else
Var Q55 = Category 5 drop-down Value(1-9 or N/A) * S5
End If

Var Q66 = Category 6 drop-down Value(1-9) * S6
Var Q77 = Category 7 drop-down Value(1-9) * S7
Var Q88 = Category 8 drop-down Value(1-9) * S8
Var Q99 = Category 9 drop-down Value(1-9) * S9

If (Category 5 = "N/A") THEN
Var FinalScore = round(((Q11 + Q22 + Q33 + Q44 + Q66 + Q77 + Q88 + Q99)
/ D5),2)
else
Var FinalScore = round((Q11 + Q22 + Q33 + Q44 + Q55 + Q66 + Q77 + Q88 +
Q99),2)
end if

Can anyone help me converting this to javascript?
Thanks so much

pmarisole, Aug 11, 2006

2. ### Lasse Reichstein NielsenGuest

"pmarisole" <> writes:

> I need help in calculating a score from a row of drop-down values.

I'm assuming you mean select elements on a HTML page. Correct me if
I'm wrong.

> I need to use the onChange to tally the score as the user moves across
> 9 categories (with drop-down selection of 1-9 or N/A) multiplied by a
> weighted score. The final column for each employee would be the
> calculated score.

Are there nine select elements for each employee?

> I know how to accomplish this with vbscript but I
> need it to process on the screen so the user can see the calculated
> score as they make selections. Also the code needs to execute as many
> times as there are employee rows.(loop) I don't know how to do this
> with Javascript
>
> Here's the VBscript with the variables involved

Can't read VBScript (especially pseudo-code, since I can't see what's
real and what's not)

> Var S1 = Category 1 Weighted score
> Var S2 = Category 2 Weighted score
> Var S3 = Category 3 Weighted score
> Var S4 = Category 4 Weighted score
> Var S5 = Category 5 Weighted score
> Var S6 = Category 6 Weighted score
> Var S7 = Category 7 Weighted score
> Var S8 = Category 8 Weighted score
> Var S9 = Category 9 Weighted score

Use an array for indexed values:

var weights = [1.1, 2.47, 3.3, ... , 0.25];

Ok, sounds like you need something like:

<select id="emp1cat1" onchange="recalc(1)">
<option>N/A</option>
<option>1</option>
...
<option>9</option>
</select>
<select id="emp1cat2" onchange="recalc(1)">
<option>N/A</option>
<option>1</option>
...
<option>9</option>
</select>
...
<select id="emp1cat9" onchange="recalc(1)">
<option>N/A</option>
<option>1</option>
...
<option>9</option>
</select>
<input type="text" id="emp1total">
<br>
<select id="emp2cat1" onchange="recalc(2)">
<option>N/A</option>
<option>1</option>
...
<option>9</option>
</select>
...

and then a function:

<script type="text/javascript">
var weights = [1.11, 0.92, ... , 12.7];
var d5 = 0.92;
function recalc(empNumber) {
var empCode = "emp" + empNumber;
var total = 0;
var isCat5set = false;
for(var i = 0; i < 9; i++) {
var select = document.getElementById(empCode+"cat"+(i+1));
var value = select.options[select.selectedIndex].value;
if (value != "N/A") {
total += Number(value) * weights;
if (i == 4) {
isCat5set = true;
}
}
}
if (!isCat5set) {
total /= d5;
}
document.getElementById(empCode + "total").value = total.toFixed(2);
}
</script>

> Can anyone help me converting this to javascript?

Oh, I'd rather not convert that when it can be done so much simpler

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

Lasse Reichstein Nielsen, Aug 11, 2006