javascript 在javascript中使用for循环计算平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9032040/
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
calculating average using for loop in javascript
提问by Nic Meiring
function averageCalculator (numvalues) {
for(i=0, i <= numvalues, i++>) {
var score = prompt("input the score")
result1 += score;
}
alert(result1 / 3);
}
this function is later triggered by a button with onclick="averageCalculator (2)
此功能稍后由一个按钮触发 onclick="averageCalculator (2)
<input type="button" value="Click for the average" onclick="averageCalculator (2)">
any ideas why its not working? it should prompt you for 2 values and then alert you with the average. not sure whats wrong.
任何想法为什么它不起作用?它应该提示您输入 2 个值,然后用平均值提醒您。不知道出了什么问题。
回答by Till
Your code has multiple issues. The for loop is not well formatted and you need to terminate statements with a semi-colon. Also you need to declare variables. And your loop will run numvalues+1 times which is why i removed the = in your loop. Also if you want to calculate an average you want to divide by numvalues.
您的代码有多个问题。for 循环格式不正确,您需要用分号终止语句。您还需要声明变量。并且您的循环将运行 numvalues+1 次,这就是我删除循环中的 = 的原因。此外,如果您想计算平均值,则需要除以 numvalues。
function averageCalculator (numvalues) {
var result1 = 0;
for(i=0; i < numvalues; i++) {
var score = prompt("input the score");
result1 += score;
}
alert(result1 / numvalues);
}
On top of the invalid syntax you will run into a common "problem" with javascript here. The inputs are treated as strings and instead of being added they will be concatenated. Providing 2 and 2 as scores will result in 11. 2 concatenated with 2 = 22 / 2 = 11. You need to cast the value to a number explicitly before adding them together:
除了无效的语法之外,您还会在这里遇到 javascript 的常见“问题”。输入被视为字符串,而不是添加它们将被连接。提供 2 和 2 作为分数将导致 11. 2 与 2 = 22 / 2 = 11 连接。在将它们加在一起之前,您需要明确地将该值转换为一个数字:
function averageCalculator (numvalues) {
var result1 = 0;
for(i=0; i < numvalues; i++) {
var score = prompt("input the score");
result1 += Number(score);
}
alert(result1 / numvalues);
}
Above code will correctly return 2
上面的代码将正确返回 2
回答by JW.
The syntax of your for-loop is wrong:
for 循环的语法是错误的:
for(i=0, i <= numvalues, i++>) {
should be
应该
for(i=0; i <= numvalues; i++) {
Tip: Also, it's better to use
提示:另外,最好使用
for(var i=0; i <= numvalues; i++) {
since then i
will be a local variable instead of a global one.
从那时起i
将是局部变量而不是全局变量。
回答by Pranav
Try like this
像这样尝试
for(var i=0; i <= numvalues; i++){}
for(var i=0; i <= numvalues; i++){}
回答by helpermethod
An alternative solution (using a functional programming libary, like Underscore.js):
另一种解决方案(使用功能的编程libary,像Underscore.js):
function averageCalculator(numValues) {
var numbers = _.map(_.range(numValues), function(element) {
return +prompt('input the score');
});
var result = _.reduce(numbers, function(memo, number) {
return memo + number;
}, memo);
alert(result / 3);
}
While a little bit more complicated (and less efficient), you'll get rid of loops altogether.
虽然有点复杂(而且效率较低),但您将完全摆脱循环。
EDIT
编辑
The +prompt('input the score')
does effectivly the same as Number(prompt('input the score'))
.
该+prompt('input the score')
做用途不同一样Number(prompt('input the score'))
。