[英]How do I modify this function
I modify this function but I want to be sure this is right because it suppose to take me to the next code question but it didn't even after it printed the result as correct.我修改了这个 function 但我想确定这是正确的,因为它假设带我进入下一个代码问题,但即使在它打印结果正确之后它也没有。
Modify this Function to return avg修改此 Function 以返回 avg
function getAverageTestScore(scores) {
let testScores = (scores[0] + scores[1]) / 2
return testScores;
}
/* Do not modify code below this line */
const avg = getAverageTestScore([80, 100]);
console.log(avg, '<-- should be 90');
Try this function, it will accept unlimited number of inputs and return average of them试试这个 function,它将接受无限数量的输入并返回它们的平均值
function getAverageTestScore(...scores) { let result = 0; for(let score of scores) { result += score; } return result / scores.length; } let avg = getAverageTestScore(2, 2, 2); console.log(avg) // 2
The scorer is probably running your function with some other inputs, so you probably need to modify it to accept arrays with length other than 2. Use a loop to sum the values, then divide by the number of elements.记分员可能正在运行您的 function 和一些其他输入,因此您可能需要对其进行修改以接受长度不是 2 的 arrays。使用循环对这些值求和,然后除以元素的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.