Solved

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

Posted on 2010-09-20
10
453 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
[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
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

As with any other System Center product, the installation for the Authoring Tool can be quite a pain sometimes. This article serves to help you avoid making these mistakes and hopefully save you a ton of time on troubleshooting :)  Step 1: Make sur…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

738 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