JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<h4>1,3,9,6,5,7,12,32</h4>
<input type="text" id="valueList"><button type="button" id="calculate">Calculate Stats</button>
<br><br>
<H2>Results</H2>
<ul id="results"><!--javascript will write items here--></ul>
JavaScript
$( "#calculate" ).click(processValues);
function processValues() {
var valueSum = 0;
var valueAverage = 0;
var valueMax = 0;
var valueMin = Infinity;
//listens for click event
$("#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)
//
//Use a loop (or loops) here to help calculate the sum, average, max, and min of the values
//
// loop to find sum
for (var i=0; i <= valueCount; i++)
{
if (value[i] != undefined) // this check exists because your array includes undefined and it shouldn't
{
valueSum += +value[i];
}
}
// praxis to find average
valueAverage = valueSum / valueCount;
// loop to find max
for (var i=0; i <=valueCount; i++)
{
if (value[i] != undefined) // this check exists because your array includes undefined and it shouldn't
{
if (value[i] > valueMax)
{
valueMax = value[i];
}
}
}
//loop to find min
for (var i=0; i <=valueCount; i++)
{
if (value[i] != undefined) // this check exists because your array includes undefined and it shouldn't
{
if (value[i] <= valueMin)
{
valueMin = value[i];
}
}
}
$("#results" ).append( "<li>The values sum is : " + valueSum + ".</li>" );//appends values
$("#results" ).append( "<li>The values average is : " + valueAverage + ".</li>" );//appends values
$("#results" ).append( "<li>The values max is : " + valueMax + ".</li>" );//appends values
$("#results" ).append( "<li>The values min is : " + valueMin + ".</li>" );//appends values
$("#results" ).append( "<li>There are " + valueCount + " values.</li>" );//appends value count
//need to append Sum, average, max, and min to bullet list here
}