Solved

# percentages coming out above 100%...

Posted on 2004-04-27
149 Views
Hi All,

I have a buggy JS function(s).
I'm testing only on IE 5.0 and up Windows 2000 and XP

Three Important fields are:
TotalDone = current count of how many boxes are checked
TotalCases = static count of how many boxes there are on the page(this always shows correctly)
TotalComplete = percentage of total checkboxes checked

I have two functions, twoDecimals() and setTotalValuesOnClick().  These functions are called from an onclick event of a checkbox with the code...
var trueorfalse = this.checked;
setTotalValuesOnClick(trueorfalse);

Here they are:
//When a user clicks on a check box the total checked is incremented and the
//percentage is figured
function setTotalValuesOnClick(selectThis)
{
//should be true or false
var a = selectThis
//tot = total amount completed so far
var tot =      parseInt(document.forms[0].TotalDone.value);
//static total number of check boxes used
var totalnumberofChkBoxes = document.forms[0].TotalCases.value;
//if it's checked
if (a==true) {
// increase total done by one.
tot = tot + 1;
document.forms[0].TotalDone.value = tot;
//if total # check boxes is not 0.
if (totalnumberofChkBoxes != 0){
//set %complete
document.forms[0].TotalComplete.value = twoDecimals((tot/totalnumberofChkBoxes)*100);
}
else {document.forms[0].TotalComplete.value = "err";}
}
else{
tot = tot - 1;
document.forms[0].TotalDone.value = tot;
if (totalnumberofChkBoxes != 0){
document.forms[0].TotalComplete.value = twoDecimals((tot/totalnumberofChkBoxes)*100);
}
else {document.forms[0].TotalComplete.value = "err";}
}
}

//rounds to two decimal places, passed in a number.
function twoDecimals(n) {
var s = "" + Math.round(n * 100) / 100
var i = s.indexOf('.')
if (i < 0) return s + ".00"
var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
if (i + 2 == s.length) t += "0"
return t
}

Thanks for all input.  This one as it turns out is live as it was a rushed project.
Yoshi
0
Question by:yoshi78
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 1

Author Comment

ID: 10950240
Solved.  It was another factor.  An explode function was used on the checkbox.  Default in the system we use.  Hence one of the entries was exploded and came out as being checked three times...Go figure.
0

Accepted Solution

modulo earned 0 total points
ID: 10950347
Closed, 300 points refunded.
modulo
Community Support Moderator
Experts Exchange
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkelâ€¦
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get â€¦
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
###### Suggested Courses
Course of the Month11 days, 17 hours left to enroll