Link to home
Start Free TrialLog in
Avatar of Jose Rivera-Hernandez
Jose Rivera-HernandezFlag for United States of America

asked on

Statistic Calculator using Javascript

Hello,

I have some code that I'd like to adjust to create a statistics calculator, the javascript should calculate the sum, average, max and min value when I entered a list of numbers on a web page; I'd like to use loops.  The results should be added to a list in the web page.


var i, n
var valueCount = 0;
var valueSum = 0;
var valueAverage = 0;
var valueMax = 0;
var valueMin = 0;

$( "#calculate" ).click(processValues);

function processValues() {
  $("#results" ).html( "" );//clears any list items from last calculation
  var valueString = $( "#valueList" ).val();
  var value = $.map(valueString.split(","), Number ); //this is an array
  valueCount = value.length; //get the lenght of the array (number of values)

 function sum() {
   sumofvalueString=0;
   valueString= document.GetElementById("#valueList").value.split(",");
   for(i=0; i <valueString.length; i++) {
     sumofvalueString += parseInt(valueString (i));
   }
  document.GetElementById("valueSum").innerHTML = sumofvalueString;
 }
 
  $("#results" ).append( "<li>The values entered: " + valueString + ".</li>" );
  $("#results" ).append( "<li>There are " + valueCount + " values.</li>" );
  $("#results" ).append( "<li>The Sum is " + valueSum + ".</li>" );
  $("#results" ).append( "<li>The Max is" + valueMax + ".</li>" );
  $("#results" ).append( "<li>The Min is" + valueMin + ".</li>" );
 
 

  $("#valueList").val("");
}

here is the code for the webpage

<h4>Enter numbers separated by commas and click calculate </h4>
        <input id="valueList" type="text" ><button type="button" id="calculate">Calculate Stats</button>
        <br><br>
        <H2>Results</H2>


Thanks,
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

I don't see a question in your post? Are you getting an error you need help with? This sounds more like a work request than a request for assistance.
Avatar of Jose Rivera-Hernandez

ASKER

The code is not working, when I entered the digits and click Calculate, I get nothing. I'd like to simplify the code using loops to calculate the max, min, average, and count. How do i do this? I tried to edit the question, but couldn't.
ASKER CERTIFIED SOLUTION
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hello,

I looked at it and it works, I added the average, but I am having problems getting the Max and Min, but can't seem to get it. HEre is the code that I am using.

 function max() {
			maxofvalueString=value[0];
				valueString= document.getElementById("valuelist").value.split(",");
				  for(i=0; i <valueString.length; i++) 
	   {
                if(max <valueString[i])
                   max= valueString[i];
			   }
	   } 

Open in new window


thanks,
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I thought about parseInt and using maxvaluestring, you are correct on the index. Thanks I got it!
Thanks!