Solved

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

Posted on 2010-09-20
10
448 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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
 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This collection of functions covers all the normal rounding methods of just about any numeric value.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

803 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