【问题标题】:Javascript Calculator (having difficulty)Javascript计算器(有困难)
【发布时间】:2016-11-14 21:44:23
【问题描述】:

我正在上入门编码课程,我们刚刚开始学习 javascript。我们的第一个任务是创建一个简单的 JavaScript 计算器,它能够计算数字列表的总和、平均值、最大值和最小值。我的老师说这可以通过多种方式完成,但他推荐了一种叫做“for”循环的东西。我在 css 和 html 方面表现不错,但我一直在努力使用 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

【问题讨论】:

标签: javascript calculator


【解决方案1】:

好的,想想如果给你一个数字列表并想计算其中的每一个,你会在逻辑上做什么。

例如,您有一张纸,上面有数字列表,并且您想弄清楚哪个是最大数字,您要做的就是查看第一个数字,将其记在脑海中,然后继续扫描在列表中向下,直到找到一个大于该数字的数字。然后,您将继续扫描列表,直到找到一个大于该数字的数字,等等,直到您到达列表的末尾。这可以通过这样的 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循环”,看看你是否可以自己实现其余的。没有比实践更好的学习方式了。

【讨论】:

    【解决方案2】:

    这就是答案。

    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
    
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      相关资源
      最近更新 更多