-1

I am in an introductory coding course and we have just begun learning about javascript. Our first assignment is to create a simple javascript calculator that is capable of computing the sum, average, max and min values of a list of numbers. My teacher says that this can be accomplished in a variety of ways but he recommends something called "for" loops. I am decent at css and html but I have been really struggling with javascript. Any help would be appreciated.

html:

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

jss:

var valueSum = 0;
var valueAverage = 0; 
var valueMax = 0;
var valueMin = 0; 

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

function processValues() {//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
  //



  $("#results" ).append( "<li>The values entered: " + valueString + ".</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

  //clears text field for next set of values to be entered
  $("#valueList").v
  • Please explain exactly what issue or question you have. – Scott Marcus Nov 14 '16 at 21:46
  • You seem to be confusing Stack Overflow with a tutorial service – Tibrogargan Nov 14 '16 at 21:47
  • This question has been answered several times, please try to google your problem before asking a question. [Here's a similar question I answered last week.](http://stackoverflow.com/questions/40433498/how-do-i-compute-an-array-or-string-of-numbers-and-mathematical-operators/40433780#40433780) – I wrestled a bear once. Nov 14 '16 at 21:48
  • Here is a link to some documentation on for loops, which should help. Goodluck! http://www.w3schools.com/js/js_loop_for.asp – holtc Nov 14 '16 at 21:48

2 Answers2

0

OK, think about what you would do logically if you were given a list of numbers and wanted to calculate each of these.

e.g., you have a piece of paper with a list of numbers on, and you wanted to figure out which is the maximum number, what you would do is look at the first number, keep that in your head, then keep scanning down the list until you find a number larger than that. You would then continue scanning the list until you found a number larger than that, etc., until you got to the end of the list. This can be implemented with a for loop like so

var maxSeen = value[0];

for (var i = 1; i < valueCount; i++) {
    var thisNumber = value[i];

    if (thisNumber > maxSeen) {
        maxSeen = thisNumber;
    }
}

console.log("max", maxSeen);

Think about how you would calculate each of the others with a pen and paper, then Google "for loops" and see if you can implement the rest yourself. There's no better way to learn than doing.

0

Here is the answer.

https://jsfiddle.net/5a3bndy9/

$( "#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

  }
Athanasios Emmanouilidis
  • 2,084
  • 3
  • 18
  • 30