2016-11-14 76 views
-1

我在編程入門課程,我們剛剛開始學習JavaScript。我們的第一項任務是創建一個簡單的JavaScript計算器,它能夠計算數字列表的總和,平均值,最大值和最小值。我的老師說這可以通過各種方式來完成,但他建議了一種叫做「for」循環的東西。我體面的CSS和HTML,但我一直在努力與JavaScript。任何幫助,將不勝感激。Javascript計算器(有難度)

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

請解釋一下你有什麼問題或疑問。 –

+0

您似乎很困惑堆棧溢出與教程服務 – Tibrogargan

+0

此問題已被回答多次,請在提問前嘗試google您的問題。 [這是我上週回答的一個類似問題。](http://stackoverflow.com/questions/40433498/how-do-i-compute-an-array-or-string-of-numbers-and-mathematical-operators/ 40433780#40433780) –

回答

0

OK,想想如果給你號的列表,想要計算出各的這些,你會在邏輯上做什麼。

例如,你有一張紙上有一列數字,你想知道哪一個是最大數字,你要做的是看第一個數字,把它保留在你的腦海中,然後保持向下掃描列表,直到找到一個比這更大的數字。然後,您將繼續掃描列表,直到找到一個大於該值的數字,等等,直到您到達列表的末尾。這可以通過實現for循環,像這樣

var maxSeen = value[0]; 

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

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

console.log("max", maxSeen); 

想想你將如何計算每個人的以「for循環」一支筆和紙,然後谷歌,看看你能自己實現的其餘部分。沒有比學習更好的方法學習了。

0

這是答案。

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 

    }