简体   繁体   English

JavaScript - 试图弄清楚为什么我的平均总和不起作用?

[英]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.

相关问题 试图弄清楚为什么 function 不工作 - trying to figure out why the function isn't working 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 无法弄清楚为什么javascript无法在我的php文件中工作 - Can't figure out why javascript isn't working in my php file 无法弄清楚为什么我的div输出不起作用 - Cant figure out why my div output isn't working 试图在 Javascript 中创建一个“trie”。 无法弄清楚为什么我的方法没有正确添加孩子 - Trying to create a 'trie' in Javascript. Can't figure out why my method isn't adding the children correctly 我不知道为什么我的JavaScript无法正常工作…我正在将值动态传递给标签 - I can't figure out why my javascript isn't working… I'm dynamically passing values to a tag 无法弄清楚为什么我的Javascript没有使用Bulma框架更改CSS - Can't figure out why my css isn't being changed by my Javascript using Bulma framework 无法弄清楚为什么它不起作用。 javascript调用数据键 - Can't figure out why it isn't working. javascript calling data-key 无法弄清楚为什么javascript不存储我的变量 - Can't figure out why javascript isn't storing my variable 我不知道为什么我的石头,剪刀布游戏中的得分不起作用 - I can't figure out why the scoring isn't working in my rock, paper, scissors game
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM