Need Help With Calculation

P

pmarisole

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
 
L

Lasse Reichstein Nielsen

pmarisole said:
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);
}
Can anyone help me converting this to javascript?

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

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top