Solved

Compute the total of 3 textbox and put it in a different textbox

Posted on 2010-09-20
10
445 Views
Last Modified: 2012-06-22
Please, bear with me, as this is my first time to do this. I just want to compute the average of the 3 textbox and put in a different textbox.

Evalgo Average Score should be computed as the average of the values of Evalgo Product Score, Evalgo Project Score and Evalgo Requirements Score. However, if any of the other 3 values is blank, do not include this in the computation. Samples below:

Sample 1            
Evalgo Product Score      3      
Evalgo Requirements Score      4      
Evalgo Project Score      5      
Evalgo Average Score      4      =(3+4+5)/3

Sample 2            
Evalgo Product Score      3      
Evalgo Requirements Score      4      
Evalgo Project Score             
Evalgo Average Score      3.5      =(3+4)/2

One more thing... the value of the Evalgo Average Score should be change or computed upon the textbox Onchange Event of  Evalgo Product Score, Evalgo Project Score and Evalgo Requirements Score.

9-20-2010-2-51-18-PM.png
0
Comment
Question by:PagodNaUtak
10 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33714512
try this code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<script>
		function score()
		{
			var averageScore = (getValue("EPdS") + getValue("EPjS") + getValue("ERS"))/3;
			document.getElementById("EAS").value = averageScore;
		}
		function getValue(id)
		{
			return parseInt(document.getElementById(id).value);
		}
	</script>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
	Evalgo Product Score <input type="text" id="EPdS"\>
	Evalgo Project Score <input type="text" id="EPjS"\>
	<br>
	Evalgo Requirement Score <input type="text" id="ERS"\>
	Evalgo Average Score <input type="text" id="EAS"\>
	<br>
	<a href="javascript: score()">get average score</a>

</BODY>
</HTML>

Open in new window

0
 
LVL 8

Author Comment

by:PagodNaUtak
ID: 33714557
Hi! Thank you for your suggestion but there is something that is missing:
1) Please, see example number 2.
Sample 2            
Evalgo Product Score      3      
Evalgo Requirements Score      4      
Evalgo Project Score            
Evalgo Average Score      3.5      =(3+4)/2
 
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33714571
Here
<script type="text/javascript">
function calc(theForm) {   
  var Evalgo_Product_Score      = theForm.EvalgoProductScore.value;     // 3
      Evalgo_Product_Score      = (Evalgo_Product_Score=="" || isNaN(Evalgo_Product_Score))?0:parseInt(Evalgo_Product_Score,10);
  var Evalgo_Requirements_Score = theForm.EvalgoRequirementsScore;      // 4
      Evalgo_Requirements_Score = (Evalgo_Requirements_Score=="" || isNaN(Evalgo_Requirements_Score))?0:parseInt(Evalgo_Requirements_Score,10);      
  var Evalgo_Project_Score      = theForm.EvalgoProjectScore.value;     // 5      
      Evalgo_Project_Score      = (Evalgo_Project_Score=="" || isNaN(Evalgo_Project_Score))?0:parseInt(Evalgo_Project_Score,10);
  theForm.EvalgoAverageScore.value = ((Evalgo_Product_Score+Evalgo_Requirements_Score+Evalgo_Project_Score)/3).toFixed(1);     // 4      =(3+4+5)/3
}
window.onload=function() {
  calc(document.forms[0])
}
</script>
<form>
EvalgoProductScore:        <input type="text" name="EvalgoProductScore"      value="" onKeyUp="calc(this.form)" /><br/>
EvalgoRequirementsScore:   <input type="text" name="EvalgoRequirementsScore" value="" onKeyUp="calc(this.form)" /><br/>
EvalgoProjectScore.value:  <input type="text" name="EvalgoProjectScore"      value="" onKeyUp="calc(this.form)" /><br/>      
Evalgo_Average_Score.value:<input type="text" name="EvalgoAverageScore"      value="" readonly="readonly" />
</form>

Open in new window

0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 33714582
Aha. Sorry
better:

<script type="text/javascript">
function calc(theForm) {
  var divisor = 0;   
  var Evalgo_Product_Score      = theForm.EvalgoProductScore.value;     // 3
      Evalgo_Product_Score      = (Evalgo_Product_Score=="" || isNaN(Evalgo_Product_Score))?0:parseInt(Evalgo_Product_Score,10);
  if (Evalgo_Product_Score) divisor++;       
  var Evalgo_Requirements_Score = theForm.EvalgoRequirementsScore.value;      // 4
      Evalgo_Requirements_Score = (Evalgo_Requirements_Score=="" || isNaN(Evalgo_Requirements_Score))?0:parseInt(Evalgo_Requirements_Score,10);
  if (Evalgo_Requirements_Score) divisor++; 
  var Evalgo_Project_Score      = theForm.EvalgoProjectScore.value;     // 5      
      Evalgo_Project_Score      = (Evalgo_Project_Score=="" || isNaN(Evalgo_Project_Score))?0:parseInt(Evalgo_Project_Score,10);
  if (Evalgo_Project_Score) divisor++;
  theForm.EvalgoAverageScore.value = (divisor==0)?0:((Evalgo_Product_Score+Evalgo_Requirements_Score+Evalgo_Project_Score)/divisor).toFixed(1);     // 4      =(3+4+5)/3
}
window.onload=function() {
  calc(document.forms[0])
}
</script>
<form>
EvalgoProductScore:        <input type="text" name="EvalgoProductScore"      value="" onKeyUp="calc(this.form)" /><br/>
EvalgoRequirementsScore:   <input type="text" name="EvalgoRequirementsScore" value="" onKeyUp="calc(this.form)" /><br/>
EvalgoProjectScore.value:  <input type="text" name="EvalgoProjectScore"      value="" onKeyUp="calc(this.form)" /><br/>      
Evalgo_Average_Score.value:<input type="text" name="EvalgoAverageScore"      value="" readonly="readonly" />
</form>

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33714616
try this

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<script>
		function score()
		{
			var totalScore = 0;
			var totalVars = 0;
			var val = getValue("EPdS");
			if ( val  )
			{
				totalScore += val;
				totalVars++;
			}
			val = getValue("EPjS");
			if ( val )
			{
				totalScore += val;
				totalVars++;
			}
			val = getValue("ERS");
			if ( val )
			{
				totalScore += val;
				totalVars++;
			}
			alert(totalScore);
			alert(totalVars);
			var averageScore = totalScore/totalVars;
			document.getElementById("EAS").value = averageScore;
		}
		function getValue(id)
		{
			var val = parseFloat(document.getElementById(id).value);
			return val;
		}
	</script>
</HEAD>

<BODY BGCOLOR="#FFFFFF">
	Evalgo Product Score <input type="text" id="EPdS"\>
	Evalgo Project Score <input type="text" id="EPjS"\>
	<br>
	Evalgo Requirement Score <input type="text" id="ERS"\>
	Evalgo Average Score <input type="text" id="EAS"\>
	<br>
	<a href="javascript: score()">get average score</a>

</BODY>
</HTML>

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33714621
Ah. missed a .value

<script type="text/javascript">
function calc(theForm) {
  var divisor = 0;   
  var Evalgo_Product_Score      = theForm.EvalgoProductScore.value;     // 3
      Evalgo_Product_Score      = (Evalgo_Product_Score=="" || isNaN(Evalgo_Product_Score))?0:parseInt(Evalgo_Product_Score,10);
  if (Evalgo_Product_Score!=0) divisor++;
  var Evalgo_Requirements_Score = theForm.EvalgoRequirementsScore.value;      // 4
      Evalgo_Requirements_Score = (Evalgo_Requirements_Score=="" || isNaN(Evalgo_Requirements_Score))?0:parseInt(Evalgo_Requirements_Score,10);
  if (Evalgo_Requirements_Score!=0) divisor++;
  var Evalgo_Project_Score      = theForm.EvalgoProjectScore.value;     // 5      
      Evalgo_Project_Score      = (Evalgo_Project_Score=="" || isNaN(Evalgo_Project_Score))?0:parseInt(Evalgo_Project_Score,10);
  if (Evalgo_Project_Score!=0) divisor++;

  var total = Evalgo_Product_Score+Evalgo_Requirements_Score+Evalgo_Project_Score;
  var average = (divisor==0)?0:total/divisor;
  theForm.EvalgoAverageScore.value = average.toFixed(1);     // 4      =(3+4+5)/3
}
window.onload=function() {
  calc(document.forms[0])
}
</script>
<form>
EvalgoProductScore:        <input type="text" name="EvalgoProductScore"      value="" onKeyUp="calc(this.form)" /><br/>
EvalgoRequirementsScore:   <input type="text" name="EvalgoRequirementsScore" value="" onKeyUp="calc(this.form)" /><br/>
EvalgoProjectScore.value:  <input type="text" name="EvalgoProjectScore"      value="" onKeyUp="calc(this.form)" /><br/>      
Evalgo_Average_Score.value:<input type="text" name="EvalgoAverageScore"      value="" readonly="readonly" />
</form>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33714630
Gurvinder: You should have value on your fields and test for NaN
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33714660
check line number 10, 16, and 22. the if condition is true only when it is not NaN.
0
 

Expert Comment

by:rcbcool
ID: 33715134
Check the below code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Test Document</title>

	<script>

		function score()

		{

			var fieldCount = 0;

			var valueTotal = 0;

			var valueAvg = 0;

			if(isNaN(getValue("EPdS")) == false){ fieldCount++; valueTotal += getValue("EPdS");};

			if(isNaN(getValue("EPjS")) == false){ fieldCount++; valueTotal += getValue("EPjS");};

			if(isNaN(getValue("ERS")) == false){ fieldCount++; valueTotal += getValue("ERS");};

			if(fieldCount > 0){

				valueAvg = (valueTotal)/fieldCount;

			}

			document.getElementById("EAS").value = valueAvg;

		}

		function getValue(id)

		{

			return parseInt(document.getElementById(id).value);

		}

	</script>

</head>



<BODY BGCOLOR="#FFFFFF">

	Evalgo Product Score <input type="text" id="EPdS"\>

	Evalgo Project Score <input type="text" id="EPjS"\>

	<br>

	Evalgo Requirement Score <input type="text" id="ERS"\>

	Evalgo Average Score <input type="text" id="EAS"\>

	<br>

	<a href="javascript: score()">get average score</a>

</body>

</html>

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33715308
@rcbcool Welcome to EE, Please do not stitch together other people's suggestions and post as your own.
 
Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Sep 22nd 2014 Microsoft released Update Rollup 1 for Microsoft Dynamics CRM 2013 Service Pack 1 and back in July Update Rollup 3 was released.  So we now have:   Update Rollup 1Update Rollup 2Update Rollup 3Service Pack 1Update Rollup 1 for S…
This collection of functions covers all the normal rounding methods of just about any numeric value.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

757 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

20 Experts available now in Live!

Get 1:1 Help Now