Solved

percentages coming out above 100%...

Posted on 2004-04-27
2
144 Views
Last Modified: 2010-04-06
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:
JS header
//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
Comment
Question by:yoshi78
2 Comments
 
LVL 1

Author Comment

by:yoshi78
Comment Utility
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

by:
modulo earned 0 total points
Comment Utility
Closed, 300 points refunded.
modulo
Community Support Moderator
Experts Exchange
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now