admin管理员组文章数量:1287595
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)
<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.
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)
<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.
Share Improve this question asked Jan 27, 2012 at 10:39 Nic MeiringNic Meiring 8825 gold badges16 silver badges33 bronze badges 3- What is happening now? Wrong answer or code not executing? – bnieland Commented Jan 27, 2012 at 10:42
-
1
You probably want to change the alert to
alert(result1 / numvalues);
– Richard Dalton Commented Jan 27, 2012 at 10:46 - im getting ReferenceError: averageCalculator is not defined so code not executing. seems to be defined as a function though... not sure – Nic Meiring Commented Jan 27, 2012 at 10:50
4 Answers
Reset to default 4Your 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
.
function averageCalculator (numvalues) {
var result1 = 0;
for(let i=0; i < numvalues; i++) {
var score = prompt("input the score");
result1 += score;
}
alert(result1 / numvalues);
}
numvalues
represents the number of inputs you want to calculate average for.
On top of the invalid syntax you will run into a mon "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:
function averageCalculator (numvalues) {
var result1 = 0;
for(i=0; i < numvalues; i++) {
var score = prompt("input the score");
result1 += Number(score);
}
alert(result1 / numvalues);
}
The code above will correctly return 2.
The syntax of your for-loop is wrong:
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.
Try like this
for(var i=0; i <= numvalues; i++){}
An alternative solution (using a functional programming libary, like 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 plicated (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'))
.
本文标签: functioncalculating average using for loop in javascriptStack Overflow
版权声明:本文标题:function - calculating average using for loop in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741247676a2365197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论