JavaScript 平均函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21708965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript average function
提问by MG_72
I am currently in a web programming class that is taught by a business professor. Naturally he doesn't know much about code and just points me to the book (which is a giant mess of spaghetti everywhere).
我目前正在上一门由商业教授教授的网络编程课程。自然地,他对代码知之甚少,只是给我指了指这本书(到处都是一大堆意大利面条)。
The exact assignment I am having issues with is here in in quotes. Hopefully this doesn't look too messy.
我遇到问题的确切分配在引号中。希望这看起来不会太乱。
DATA AVAILABLE
可用数据
There is a local storage value you will need named: qualifier
There is a local storage value you will need named: factor
There is an array you will need named: sales
The array and local storage exist so just use them.
有一个你需要的本地存储值,命名为:qualifier 你需要
一个本地存储值,命名为:factor
有一个你需要命名
的数组:sales数组和本地存储存在,所以只需使用它们。
WHAT TO RETURN
返回什么
Return the average bonus.
返回平均奖金。
HOW TO CALCULATE AVERAGE BONUS
如何计算平均奖金
- Each amount in the array sales[] that is more than qualifier gets a bonus.
Remember: array means you think for() loop.- The bonus is calculated as that sales amount times factor.
- If the sale is greater than qualifier add the bonus to the bonus total.
- Calculate the average by dividing the total by the number of bonuses that qualify.
Remember: you are returning the average of bonuses that qualify; not the total of bonuses.
- 数组 sales[] 中超过限定符的每个金额都会获得奖励。
请记住:数组意味着您认为 for() 循环。- 奖金计算为销售额乘以系数。
- 如果销售额大于限定符,则将奖金添加到奖金总额中。
- 通过将总数除以符合条件的奖金数量来计算平均值。
请记住:您返回的是符合条件的奖金的平均值;不是奖金总数。
I understand that this site is not for you folks to simply do my homework for me, I do not expect that to happen. Below is the current code of what I have. I should note that the teacher has us submit our code into a console that is developed by the university. The reason I say this is because in this assignment, certain local storage variables are already defined, as well as the array. I have been trying to test this function out using notepad and saving it in an HTML document and testing it with a browser. You will notice that my code below is written to be tested with a browser, and that I have defined the array and localstorage variables myself. Hope that makes sense.
我知道这个网站不适合你们这些人简单地为我做功课,我不希望这种情况发生。以下是我所拥有的当前代码。我应该注意到老师让我们将我们的代码提交到由大学开发的控制台。我之所以这么说,是因为在这个赋值中,已经定义了某些本地存储变量,以及数组。我一直在尝试使用记事本测试此功能并将其保存在 HTML 文档中并使用浏览器进行测试。你会注意到我下面的代码是为了用浏览器测试而编写的,而且我自己定义了数组和 localstorage 变量。希望这是有道理的。
Here is my JavaScript:
这是我的 JavaScript:
sales = new [Array]();
sales[0] = 3;
sales[1] = 167;
sales[2] = 191;
sales[3] = 1;
sales[4] = 45;
localStorage.qualifier=5;
localStorage.factor=2;
total = 0;
function myFunction() {
for (i=0;i<sales.length;i++){
if (sales[i]>localStorage.qualifier) {
bonus = sales[i]*localStorage.factor;
total = total + bonus;
};
avg = total/bonus;
};
return avg;
};
myFunction();
alert(extraPay())
I want to write the function to calculate the average bonus. I tried using this web-based text editor from repl.it, and am getting a type error saying the object is not a function. I googled the error, which led me to this site and subsequently creating an account. Thank you so much for your time, if you could point out where my code goes wrong, or if I am just crazy all over the place, that would really help. Thanks! Sorry for the excruciatingly long post.
我想编写函数来计算平均奖金。我尝试使用 repl.it 中的这个基于 Web 的文本编辑器,但收到一个类型错误,指出该对象不是函数。我在谷歌上搜索了错误,这导致我访问了该站点并随后创建了一个帐户。非常感谢您抽出宝贵时间,如果您能指出我的代码哪里出错了,或者我只是疯了,那真的很有帮助。谢谢!抱歉,帖子太长了。
回答by Bic
Your error is likely because of this line:
你的错误很可能是因为这一行:
alert(extraPay());
You are alerting to a method that does not exist, unless it is defined elsewhere.
您正在警告一个不存在的方法,除非它在别处定义。
Additionally, it is much faster to declare arrays using just bracket notation:
此外,仅使用括号表示法声明数组要快得多:
var sales = [];
If you are going to use the object creation notation, you should do this:
如果您打算使用对象创建符号,您应该这样做:
var sales = new Array();
Finally, based on the assignment, and your code, I don't think you will get what you want. You are calculating the total, and dividing it by the last bonus amount. I think what you need to do instead is calculate the total and keep track of how many times you have a bonus. Then divide total by the bonus counter.
最后,根据作业和你的代码,我认为你不会得到你想要的。您正在计算总数,并将其除以最后的奖金金额。我认为您需要做的是计算总数并跟踪您获得奖金的次数。然后将总数除以奖金计数器。
function myFunction() {
var counter = 0;
for (i=0;i<sales.length;i++){
if (sales[i]>localStorage.qualifier) {
bonus = sales[i]*localStorage.factor;
total = total + bonus;
counter++;
};
avg = total/counter;
};
return avg;
};
myFunction();
回答by Andy
Everyone's pointed out some of the syntax errors and general weirdness with your code. I will point out that you've read one of the instructions wrong:
每个人都指出了您的代码的一些语法错误和一般怪异之处。我会指出您已错误地阅读了其中一条说明:
Calculate the average by dividing the total by the number of bonuses that qualify.
通过将总数除以符合条件的奖金数量来计算平均值。
Currently you're doing this...
目前你正在这样做......
total = total + bonus;
...when you should be dividing by the number of qualifyingbonuses instead. To do that you need a new variable called bonusesQualified
:
...当您应该除以符合条件的奖金数量时。为此,您需要一个名为 的新变量bonusesQualified
:
function calculate(sales) {
var total = 0, bonusesQualified = 0;
for (i = 0; i < sales.length; i++) {
if (sales[i] > qualifier) {
bonus = sales[i] * factor;
total = total + bonus;
bonusesQualified++;
};
avg = total / bonusesQualified;
};
return avg;
};