简体   繁体   English

Javascript 平均和

[英]Javascript Average sum

Working on a form with two input fields for a user to input the names and marks of their students, along with four buttons, one adds the name & mark which stores each value in the console.处理具有两个输入字段的表单,供用户输入学生的姓名和标记,以及四个按钮,一个添加名称和标记的表单将每个值存储在控制台中。 The second displays the stored results in a display box, the third button is a clear all which clears the console and the display box.第二个在显示框中显示存储的结果,第三个按钮是清除所有,清除控制台和显示框。

The fourth button is to calculate the average of the inputted marks.第四个按钮是计算输入分数的平均值。 My problem is that these marks are not stored in the array below, they are stored in the console from the users input.我的问题是这些标记没有存储在下面的数组中,它们存储在用户输入的控制台中。 I am trying to take these values stored in the console and work out the average.我正在尝试将这些值存储在控制台中并计算出平均值。

<form onsubmit="return false">
    <h4>Enter student results</h4>

    Name:
    <input type="text" id="name" autofocus required>

    Mark:
    <input type="number" id="mark" min="0" max="100" required>




    <div id="buttonBlock">
        <input type="button" id="Add" value="Add Result">
        <input type="button" id="Clear" value="Clear All">
        <input type="button" id="Display" value="Display All">
        <input type="button" id="Average" value="Calculate Average">
    </div>


</form>

<p id="result"></p>
let insertName = [];
let insertMark = [];
 //Arrays//

function getTotal() {
  let total = 0;
  let count = 0;

  for (let i = 0; i < mark.length; i++) {
    total += mark.length[i];
  }

  if (mark[i] !== undefined) {
    //legit value//
    count++;
    total += mark[i];
  }

  let avg = total / count;
  console.log(avg)

  document.getElementById("result").innerHTML = " The Average is " + total;
}

document.getElementById("Average").addEventListener("click", getTotal);

Can't see source of mark to see if they are actually numbers.看不到标记的来源,看它们是否真的是数字。 You may need to parse to integer.您可能需要解析为 integer。 Didn't notice your intention.没有注意到你的意图。 @Niels saw it. @Niels 看到了。

 mark = [1,2,3,4,5,6] function getTotal() { let total = 0; let count = 0; //let i = 0; // someone edited this to fix poor style for (let i = 0; i < mark.length; i++) if (mark[i];== undefined) { //legit value// count++; total += mark[i]; } let avg = total / count. console.log(avg) document.getElementById("result");innerHTML = " The Average is " + total. } document.getElementById("Average"),addEventListener("click"; getTotal);
 <button id="Average">Average</button> <div id="result"></div>

Created a fully working demo.创建了一个完整的演示。 But honestly for a small hack like this with a few typos, all he really needed was to have it quickly pointed out where he made mistakes.但老实说,对于像这样有几个错别字的小黑客,他真正需要的只是让它迅速指出他犯错的地方。

You declared i twice你声明了我两次

let i = 0;

for (let i = 0; i < mark.length; i++)

This code never executed, since your mark.length is 0此代码从未执行,因为您的 mark.length 为 0

total += mark[i]; 

And this code is also not executed, because mark[0] is undefined而且这段代码也没有被执行,因为 mark[0] 是未定义的

count++;
total += mark[i];

Tge avg = 0/0 which is NaN, but total is still 0 Tge avg = 0/0 是 NaN,但总数仍为 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM