Velocity Reviews > Need Help With Calculation

# Need Help With Calculation

pmarisole
Guest
Posts: n/a

 08-11-2006
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

Lasse Reichstein Nielsen
Guest
Posts: n/a

 08-11-2006
"pmarisole" <(E-Mail Removed)> 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[i];
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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post jingyue Java 0 01-31-2008 03:34 PM sara C++ 4 10-21-2006 08:20 PM =?Utf-8?B?U2ltbw==?= ASP .Net 7 11-05-2005 05:59 PM Neredbojias HTML 3 10-04-2005 12:55 AM Ding Lei Java 9 06-25-2004 06:04 PM