简体   繁体   English

比较两个 arrays 并在项目匹配时生成分数

[英]Comparing two arrays and generating points when the items match

Here is my answer, which is wrong, but I cant figure out why.这是我的答案,这是错误的,但我不知道为什么。 The logic seems fine but my acc is returning a larger number than expected most of the time.逻辑似乎很好,但我的acc大多数时候返回的数字比预期的要大。

Here is the question:这是问题:

The first input array contains the correct answers to an exam, like ["a", "a", "b", "d"].第一个输入数组包含考试的正确答案,例如 ["a", "a", "b", "d"]。 The second one is "answers" array and contains student's answers.第二个是“答案”数组,包含学生的答案。

The two arrays are not empty and are the same length.两个 arrays 不为空,长度相同。 Return the score for this array of answers, giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer(empty string).返回此答案数组的分数,每个正确答案为 +4,每个错误答案为 -1,每个空白答案(空字符串)为 +0。

My Answer:我的答案:

function checkExam(array1, array2) {
return array1.concat(array2).reduce((acc, curr, i) => 
    curr[i] === curr[i + array1.length] ? acc + 4 : curr[i + array1.length] === '' ? acc + 0 : acc - 1, 0);

}

EDIT: I messed up variable names:P编辑:我弄乱了变量名:P

It seems easier to me separate the logic of the function in a map and a reduce.对我来说,将 map 和 reduce 中的 function 的逻辑分开似乎更容易。

const checkExam = (correctExam, answer) => {
  const total = correctExam.map((it, i) => {
    if(it === answer[i]){ return 4 }
    else if(answer[i] === ""){ return 0 }
    else{ return -1 }
  }).reduce((acc, curr) => acc + curr, 0)
  return total
}

You can also separate the map and the reduce to know how much answers are correct, incorrect or empty and even assign values dynamically to each option您还可以将 map 和 reduce 分开,以了解有多少答案是正确、不正确或空的,甚至可以为每个选项动态分配值

curr in your reduce is just the current item in this iteration of the reduce function, not the original array.你的reduce中的curr只是reduce function迭代中的当前项目,而不是原始数组。 And since you know the two arrays are the same length, there really isn't any sense in concatenating since you are just going to be wasting cycles iterating over the answers.而且由于您知道两个 arrays 的长度相同,因此连接实际上没有任何意义,因为您只会浪费循环迭代答案。 Something like this should work:像这样的东西应该工作:

 var correct = ["a", "b", "b", "c"]; var answers = ["a", "", "d", "c"]; function checkExam(answerKey, studentResponse) { return answerKey.reduce((score, answer, qIdx) => { if (answer === studentResponse[qIdx]) { score += 4; } else if (studentResponse[qIdx];== '') { score -= 1; } return score, }; 0). } console,log(checkExam(correct; answers));

Here we will iterate over the answer key ( answerKey ) and for each item ( answer ) compare it to the corresponding item at the same index ( qIdx ) in the other array studentResponse[qIdx] and score appropriately.在这里,我们将遍历答案键 ( answerKey ) 并将每个项目 ( answer ) 与另一个数组studentResponse[qIdx]中相同索引 ( qIdx ) 处的相应项目进行比较并适当评分。 Assuming that a missing answer is an empty string and not anything else (which your question seems to suggest).假设缺少的答案是空字符串而不是其他任何内容(您的问题似乎暗示了这一点)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM