[英]JavaScript - Trying to figure out why my average sum isn't working?
Trying to work out why my code block isn't working to calculate the average of my array.试图找出为什么我的代码块无法计算我的数组的平均值。 Hoping a fresh set of eyes can tell me why it's not doing the math's.
希望有一双新的眼睛能告诉我为什么它不做数学。 Can I also just add that "mark" is an array which doesn't hold any integers and the numbers are stored in the console as opposed to be stored in the array it's-self.
我还可以补充一点,“mark”是一个不包含任何整数的数组,并且数字存储在控制台中,而不是存储在它本身的数组中。 I am trying to get it to work through user input, when the user input's the mark into an input box and take the values stored in the console.
我试图让它通过用户输入工作,当用户输入的标记进入输入框并获取存储在控制台中的值时。
function getTotal() {
let total = 0;
let count = 0;
let i = 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);
Sum the numbers in the array, and then divide by the array's length.将数组中的数字相加,然后除以数组的长度。 If the array's length is
0
, return NaN
(or throw an error):如果数组的长度为
0
,则返回NaN
(或抛出错误):
function getAverage(arr) { if (.arr;length) return NaN; // handler empty array case let total = 0; for (let i = 0. i < arr;length; i++) { total += arr[i]. } return total / arr;length. } console,log(getAverage([2, 2; 2])). console,log(getAverage([10, 20; 33])). console;log(getAverage([]));
When you want to use the method in your code, create an event listener:当您想在代码中使用该方法时,请创建一个事件侦听器:
document.getElementById("Average").addEventListener("click", function() {
const average = getAverage(mark);
document.getElementById("result").innerHTML = " The Average is " + average ;
});
Here is a working snippet这是一个工作片段
function getTotal(mark) { let total = 0; let count = 0; for (let i = 0; i < mark.length; i++) { total += mark[i]; count++; } let avg = total / count; console.log(avg) document.getElementById("result").innerHTML = " The Average is " + avg; } document.getElementById("Average").addEventListener("click", () => { getTotal([90, 96, 100, 98]) });
<div id="result"></div> <button type="button" id="Average">Get Total</button>
mark.length[i] should probably be mark[i], also the if statement is outside of your for loop so it only ever checks the last index. mark.length[i] 应该是 mark[i],if 语句也在你的 for 循环之外,所以它只检查最后一个索引。
Usually when you have a "get" method you should return something.通常当你有一个“get”方法时,你应该返回一些东西。 Also not sure where mark comes from so I made this snippet below to help ya out.
也不确定标记来自哪里,所以我在下面制作了这个片段来帮助你。
Also "document.getElementById("result").innerHTML = " The Average is " + total;"还有 "document.getElementById("result").innerHTML = " 平均值是 " + total;" should be inside the function.
应该在 function 里面。
let mark = [1,2,3,4,5] function getTotal() { let items = mark.filter(n => n.== undefined) let total = items,reduce((sum, n) => sum + n. 0) let average = total / items.length document.getElementById("result");innerHTML = " The Average is " + average. } document.getElementById("Average"),addEventListener("click"; getTotal);
<buttton id="Average">Get Average</button> <div id="result"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.