简体   繁体   English

我该如何修改这个 function

[英]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.

相关问题 如何在回调函数中修改数组? - How do I modify my array in the callback function? 如何修改我的 function 添加随机源? - How do I modify my function to add a random source? 如何修改功能以在HTML onclick中播放声音? - How do I modify a function to play a sound in HTML onclick? 如何修改Ajax函数的响应URL? - How do I modify the response url of an ajax function? 如何修改/覆盖由无限setInterval调用的javascript函数? - How do I modify/overwrite a javascript function that is called by an infinite setInterval? 如何修改我的删除帐户 function 以适用于 Google 帐户? - How do I modify my delete account function to work for Google accounts? 如果给定的小数点四舍五入为整数,如何修改该函数以返回true;否则,返回false? - How do I modify a function to return true if the given decimal is even when it is rounded to an integer and false otherwise? 如何修改 react-redux 连接函数的默认名称? - How do I modify the default name of react-redux connect function? 如何修改保存功能以同时保存用户/编辑用户? - How can I modify my save function to do both save user / edit user? 如何修改引导轮播javascript文件,以便它可以触发此功能? - How do I modify the bootstrap carousel javascript file so that it can trigger this function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM