[英]javascript, returning true when it's false whenever I would try to calculate the average of parameters
function passingAverage(n){
var average = 0, i;
for (i=0; i < n.length; i+= 1){
average += n[i];
}
if ((average/n.length)<49)
return false;
else
return true;
}
in this code, I compare the numbers given in the console.log and just calculate for average.在这段代码中,我比较了 console.log 中给出的数字,并计算了平均值。
console.log('passingAverage(75,42,98) returns: ' + passingAverage(75,42,98));
console.log('passingAverage(34,93,77,89,49) returns: ' + passingAverage(34,93,77,89,49));
console.log('passingAverage(33,61) returns: ' + passingAverage(33,61));
console.log("\n");
for console.log(passing average(75,42,98) and (34,93,77,89,49) indeed does return true which is correct. But, for the console log(33,61), when the 33+61/2 isn't clearly above 49, it returns true automatically for some reason when it should return false. What could be the reason?对于 console.log(passing average(75,42,98) 和 (34,93,77,89,49) 确实返回 true,这是正确的。但是,对于控制台 log(33,61),当 33+ 61/2 并没有明显高于 49,当它应该返回 false 时,它出于某种原因自动返回 true。可能是什么原因?
That's because you're not passing an array as expected.那是因为您没有按预期传递数组。
passingAverage(33,61)
pass two parameters. passingAverage(33,61)
传递两个参数。
If you want the function to have the same signature as you define it in the example you provided, then you should call it like this:如果您希望该函数具有与您在提供的示例中定义的相同的签名,那么您应该像这样调用它:
passingAverage([33,61]); // pass an array of elements
If you want to call the funtion like passingAverage(33,61)
then the function should be defined like this:如果你想像
passingAverage(33,61)
那样调用函数,那么函数应该像这样定义:
function passingAverage(){ // no arguments, we will use the arguments array
var n = arguments; // arguments is defined for every function it's an array of all the arguments passed in
var average = 0, i;
for (i=0; i < n.length; i+= 1){
average += n[i];
}
if ((average/n.length)<49)
return false;
else
return true;
}
// then call the function like this
passingAverage(1, 65, 23, 22); // without using an array
This is a perfect example of why it's best to use debugger
rather than console.logs to debug your code.这是为什么最好使用
debugger
而不是 console.logs 来调试代码的完美示例。 Your function is always returning true
because your average is always NaN
:你的函数总是返回
true
因为你的平均值总是NaN
:
function passingAverage(n) { var average = 0, i; for (i = 0; i < n.length; i += 1) { average += n[i]; } return average / n.length } console.log('passingAverage(75,42,98) returns: ' + passingAverage(75,42,98)); console.log('passingAverage(34,93,77,89,49) returns: ' + passingAverage(34,93,77,89,49)); console.log('passingAverage(33,61) returns: ' + passingAverage(33,61));
This is because you're passing two distinct float arguments to your method, which is in fact expecting an array of values:这是因为您将两个不同的浮点参数传递给您的方法,实际上它需要一个值数组:
function passingAverage(n) { console.log("arguments:", n); var average = 0, i; for (i = 0; i < n.length; i += 1) { average += n[i]; } return average / n.length } passingAverage(75,42,98);
Simply pass your values as an array and you'll be good to go.只需将您的值作为数组传递,您就可以开始使用了。
use arguments
to loop over ALL function arguments regardless of length.无论长度如何,都使用
arguments
循环遍历所有函数参数。
function passingAverage() {
var average = 0, i;
for (i = 0; i < arguments.length; i++) {
average += arguments[i];
}
if ((average/n.length)<49)
return false;
else
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.