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,
Our community of experts have been thoroughly vetted for their expertise and industry experience.